简体   繁体   English

根据Pandas中的行值比较数据框

[英]Compare dataframes based on row values in Pandas

What is the best way to compare two datasets (.csv files) using Pandas where there is no 1:1 cardinality between the data? 在数据之间没有1:1基数的情况下,使用Pandas比较两个数据集(.csv文件)的最佳方法是什么?

For example: Here's a sample from dataset one - 例如:这是数据集中的一个样本-

#### Row Item Color Price
01 Shirt Red $30
02 Hat Blue $10

And a sample from dataframe two - 还有一个来自数据框2的样本-

#### Row Item Color Price
01 Trouser Black $20
02 Bag Yellow $ 30
03 Hat Blue $10
04 Shirt Red $30

So if I wanted to compare all rows in both datasets where there is a Shirt row . 因此,如果我想比较两个数据集中有衬衫行的所有行

what is the best way to do it? 最好的方法是什么?

I'm using Pandas/Python3.7 我正在使用Pandas / Python3.7

Thanks! 谢谢!

check this example: 检查这个例子:

DF1 = pd.DataFrame(data={'c1':['abc','abc','iop','iop'],'c2':['xyz','mno','yut','trg'],'c3':[0,0,0,0]})

    c1  c2  c3
0   abc xyz 0
1   abc mno 0
2   iop yut 0
3   iop trg 0


DF2 = pd.DataFrame(data={'c1':['iop','abc','bhj','iop','xdf'],'c2':['yut','mno','uio','yut','edc']})
    c1  c2
0   iop yut
1   abc mno
2   bhj uio
3   iop yut
4   xdf edc
match = pd.merge(DF1,DF2,on=['c1','c2'],how='inner')
print(match)

    c1  c2  c3
0   abc mno 0
1   iop yut 0
2   iop yut 0

So by using pd.merge you can get the matching rows 因此,使用pd.merge可以获取匹配的行

You can simply use pandas merge like this: 您可以像这样简单地使用熊猫merge

pd.merge(df1[df1.Item == 'Shirt'], df2[df2.Item == 'Shirt'], on=['Item','Color', 'Price')

This will produce an output only when all columns in both dataframes match for Item='Shirt' and corresponding columns are also equal. 仅当两个数据帧中的所有列都匹配Item ='Shirt'并且对应的列也相等时,才会产生输出。

Let me know if this is what you want. 让我知道这是否是您想要的。

Filter after merge merge后过滤

df1.merge(df2,on='Item').loc[lambda x : x.Item=='Shirt']
Out[89]: 
   Row_x   Item Color_x Price_x  Row_y Color_y Price_y
0      1  Shirt     Red     $30      4     Red     $30

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM