python - 如何基于列比较两个不同大小的数据框?

标签 python r pandas comparison

我有两个不同大小的数据框

df1

     YearDeci  Year  Month  Day  ...  Magnitude    Lat    Lon  
0     1551.997260  1551     12   31  ...        7.5  34.00  74.50      
1     1661.997260  1661     12   31  ...        7.5  34.00  75.00      
2     1720.535519  1720      7   15  ...        6.5  28.37  77.09      
3     1734.997260  1734     12   31  ...        7.5  34.00  75.00      
4     1777.997260  1777     12   31  ...        7.7  34.00  75.00      

df2

         YearDeci  Year  Month  Day  Hour  ...  Seconds   Mb     Lat     Lon  
0     1669.510753  1669      6    4     0  ...        0  NaN  33.400  73.200    
1     1720.535519  1720      7   15     0  ...        0  NaN  28.700  77.200    
2     1780.000000  1780      0    0     0  ...        0  NaN  35.000  77.000    
3     1803.388014  1803      5   22    15  ...        0  NaN  30.600  78.600    
4     1803.665753  1803      9    1     0  ...        0  NaN  30.300  78.800
5     1803.388014  1803      5   22    15  ...        0  NaN  30.600  78.600.

1.我想根据“YearDeci”列比较 df1 和 df2。并找出公共(public)条目和唯一条目(公共(public)行以外的行)。

2.根据'YearDeci'列输出df1中的公共(public)行(相对于df2)。

3.根据“YearDeci”列输出 df1 中唯一的行(相对于 df2)。

*注意:“YearDeci”中最多 +/-0.0001 的小数值差异是可以容忍的

预期输出如下

row_common=

      YearDeci     Year   Month  Day ...   Mb     Lat     Lon 
2     1720.535519  1720      7   15  ...  6.5  28.37  77.09

row_unique=

      YearDeci  Year  Month  Day  ...  Magnitude    Lat    Lon  
0     1551.997260  1551     12   31  ...        7.5  34.00  74.50      
1     1661.997260  1661     12   31  ...        7.5  34.00  75.00           
3     1734.997260  1734     12   31  ...        7.5  34.00  75.00      
4     1777.997260  1777     12   31  ...        7.7  34.00  75.00 

最佳答案

首先在“each with every”上比较df1.YearDecidf2.YearDeci 原则。 要执行比较,请使用 np.isclose 函数和假定的绝对值 宽容。

结果是一个 bool 数组:

  • 第一个索引 - df1 中的索引,
  • 第二个索引 - df2 中的索引。

然后,使用np.argwhere,找到True值的索引,即索引 来自 df1df2 的“相关”行,并从中创建一个 DateFrame。

执行上述操作的代码是:

ind = pd.DataFrame(np.argwhere(np.isclose(df1.YearDeci[:, np.newaxis],
    df2.YearDeci[np.newaxis, :], atol=0.0001, rtol=0)),
    columns=['ind1', 'ind2'])

然后,在两个 DataFrame 中都有指向“相关”行的索引对, 执行以下合并:

result = ind.merge(df1, left_on='ind1', right_index=True)\
    .merge(df2, left_on='ind2', right_index=True, suffixes=['_1', '_2'])

最后一步是删除两个“辅助索引列”(ind1ind2):

result.drop(columns=['ind1', 'ind2'], inplace=True)

结果(分为2部分)是:

    YearDeci_1  Year_1  Month_1  Day_1  Magnitude  Lat_1  Lon_1   YearDeci_2  \
0  1720.535519    1720        7     15        6.5  28.37  77.09  1720.535519   

   Year_2  Month_2  Day_2  Hour  Seconds  Mb  Lat_2  Lon_2  
0    1720        7     15     0        0 NaN   28.7   77.2  

关于python - 如何基于列比较两个不同大小的数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58615048/

相关文章:

r - 计算每个字符并存储为 <K,V> 对

Python Pandas Dataframe 将列值/索引拉低一

python - 如何更新子图?

python - 类型错误 : invalid destination position for blit

python - Django 多用户对一模型。关于用户的模型

python /R : generate dataframe from XML when not all nodes contain all variables?

python - 电话号码清理

python - 是否可以为小部件设置标准样式?

r - 将矩阵变换为部分平均矩阵

r - 有效地计算跨多列的字符串出现次数