简体   繁体   English

比较不同数据框中的两个相似列,并为两个数据框取未合并的行

[英]Compare two similar columns in different data frames and take unmerged rows for both data frame

I have two data frames(df1 and df2) and these two have more than 100 columns.我有两个数据框(df1 和 df2),这两个有 100 多列。 JA column is the id column. JA 列是 id 列。 I want to compare two columns at one time and get unmerged results from both data frames just like df3.我想一次比较两列,并从两个数据帧中获得未合并的结果,就像 df3. I created df3 for the BC column.我为 BC 列创建了 df3。 I want to do that for the whole data frame.我想对整个数据框这样做。 I mean I have to check every column one by one not all columns at once.我的意思是我必须一一检查每一列,而不是一次检查所有列。 When I checked a column I want to create something like df3.当我检查一列时,我想创建类似 df3 的内容。 Is there any way to do that.有没有办法做到这一点。

df1
       JA      AB     BC   fas   waa   ad
1      1       ace    52    5     2    ce
2      22      a e    3     5    78    ce
3      36      nas    4     4     5    as
4      45      kas    25    2    19    as
5      51                  25          sd
6      61      nas    45    8    32    as
7      897       a    34   13    34    qr
8      88      nas         12    0     as
9      29      jaa    1    10    45    aw
10     18      saa    34    0    98    aa

df2
       JA      AB     BC   fas   waa   ad
1      1       ace    52    5     2    ce
2      22      ace     3    5    18    ce
3      36      nas     1    4     5    as
4      45      kas    25   12    19    ms
5      51              5    5          sd
6      61      nas    45    8    32    as
7      897     paa    34   23          qr
8      88      nas    11   12     0    as
9      29       aa         10     5    aw
10     18      saa    34    0    98     a
 

df3
JA     BCdf1    BCdf2
36       4         1
51                 5
88                11
29       1

Processing flow:.处理流程:。

  1. pandas.eq() is used to create a mask by comparing two data frames. pandas.eq() 用于通过比较两个数据帧来创建掩码。
  2. create a comparison between df1 and df2 and df2 and df1.创建 df1 和 df2 以及 df2 和 df1 之间的比较。
  3. reorder the columns by joining each one.通过加入每一列来重新排序列。
maskA = df1.eq(df2)

JA  AB  BC  fas waa ad
1   True    True    True    True    True    True
2   True    False   True    True    False   True
3   True    True    False   True    True    True
4   True    True    True    False   True    False
5   True    False   False   False   False   True
6   True    True    True    True    True    True
7   True    False   True    False   False   True
8   True    True    False   True    True    True
9   True    False   False   True    False   True
10  True    True    True    True    True    False

df_1 = df1[~maskA].fillna('')
maskB = df2.eq(df1)
df_2 = df2[~maskB].fillna('')
df_1['JA'].update(df1['JA'])
df_2['JA'].update(df2['JA'])

df_all = pd.merge(df_1, df_2, on='JA', suffixes=('_df1','_df2')).sort_index(axis=1)
cols = ['JA','AB_df1','AB_df2','BC_df1','BC_df2','ad_df1','ad_df2','fas_df1','fas_df2','waa_df1','waa_df2']
df_all.loc[:,cols]

df_all
    JA  AB_df1  AB_df2  BC_df1  BC_df2  ad_df1  ad_df2  fas_df1 fas_df2 waa_df1 waa_df2
0   1                                       
1   22  a e ace                         78  18
2   36          4   1                       
3   45                  as  ms  2   12      
4   51              5           25  5       
5   61                                      
6   897 a   paa                 13  23  34  
7   88              11                      
8   29  jaa aa  1                       45  5
9   18                  aa  a               

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较具有相同列但行数不同的两个数据帧? - How to compare two data frames with same columns but different number of rows? 并排比较相同列但不同行的两个数据框 - Compare two data frames side by side for same columns but different rows Python Pandas:比较一列中的两个数据帧,并返回另一个数据帧中两个数据帧的行内容 - Python Pandas : compare two data-frames along one column and return content of rows of both data frames in another data frame 如何比较两个不同数据框中的列并保留第一个数据框中的值? - How to compare columns from two different Data Frames and keep the values from the first Data Frame? 比较 2 个不同数据框的列 - Compare columns of 2 different Data frames 将数据框的两个日期列与 python 中第二个数据框的另外两个数据框进行比较 - compare a two date columns of a data frame with another two data frames of second data frame in python 如何比较两个不同数据框中的两列并计算出现次数 - how to compare two columns in two different data frames and count the occurrences 比较两个不同的熊猫数据框中的两列值 - compare two columns values in two different pandas data frames 如何比较python中两个不同数据框的列? - how to compare columns of two different data frames in python? 如何比较两个不同数据帧的列并保持公共值 - How to compare columns of two different data frames and keep the common values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM