This question already has answers here:
Check if pandas dataframe is subset of other dataframe
                                
                                    (3个答案)
                                
                        
                                在8个月前关闭。
            
                    
我有2个csv文件(csv1,csv2)。在csv2中,csv2中可能添加了新的列或行。
我需要验证csv1是否为csv2的子集。为了成为子集,整个行应同时出现在文件和新列中的元素中,否则应忽略行。

csv1:

c1,c2,c3
A,A,6
D,A,A
A,1,A


csv2:

c1,c2,c3,c4
A,A,6,L
A,changed,A,L
D,A,A,L
Z,1,A,L
Added,Anew,line,L


我正在尝试的是:

df1 = pd.read_csv(csv1_file)
df2 = pd.read_csv(csv2_file)
matching_cols=df1.columns.intersection(df2.columns).tolist()

sorted_df1 = df1.sort_values(by=list(matching_cols)).reset_index(drop=True)
sorted_df2 = df2.sort_values(by=list(matching_cols)).reset_index(drop=True)


print("truth data>>>\n",sorted_df1)
print("Test data>>>\n",sorted_df2)


df1_mask = sorted_df1[matching_cols].eq(sorted_df2[matching_cols])
# print(df1_mask)
print("compared data>>>\n",sorted_df1[df1_mask])



它给出的输出为:

truth data>>>
   c1   c2   c3
0  A  1   A
1  A    A  6
2  D    A    A

Test data>>>
       c1       c2    c3   c4
0      A        A   6   L
1      A  changed     A  L
2  Added     Anew  line L
3      D        A     A   L
4      Z      1     A   L

compared data>>>
     c1   c2   c3
0    A  NaN  NaN
1    A  NaN  NaN
2  NaN  NaN  NaN


我想要的是:

compared data>>>
     c1   c2   c3
0    Nan  NaN  NaN
1    A    A    6
2  D    A    A


请帮忙。

谢谢

最佳答案

如果由于不匹配而需要在最后一行缺少值,请在左连接和DataFrame.merge参数中使用indicator,然后通过mask和rmove helper列_merge设置错误值:

matching_cols=df1.columns.intersection(df2.columns)

df2 = df1[matching_cols].merge(df2[matching_cols], how='left', indicator=True)
df2.loc[df2['_merge'].ne('both')] = np.nan
df2 = df2.drop('_merge', axis=1)
print (df2)

    c1   c2   c3
0    A    A    6
1    D    A    A
2  NaN  NaN  NaN

关于python - 需要检查一个数据帧是否是另一个数据帧的子集,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56748606/

10-12 21:26