简体   繁体   English

比较 Pandas 中两个大小不等的 Dataframes 中的列以进行条件检查

[英]Compare columns in Pandas between two unequal size Dataframes for condition check

I have two pandas DF.我有两只熊猫 DF。 Of unequal sizes.大小不等。 For example :例如 :

Df1
id     value
a      2
b      3
c      22
d      5 

Df2 
id     value
c      22
a      2

No I want to extract from DF1 those rows which has the same id as in DF2.不,我想从 DF1 中提取与 DF2 具有相同 id 的那些 Now my first approach is to run 2 for loops, with something like :现在我的第一种方法是运行 2 个 for 循环,类似于:

x=[]
for i in range(len(DF2)):
    for j in range(len(DF1)):
        if DF2['id'][i] == DF1['id'][j]:
          x.append(DF1.iloc[j])    

Now this is okay, but for 2 files of 400,000 lines in one and 5,000 in another, I need an efficient Pythonic+Pnadas way现在这没问题,但是对于 2 个 400,000 行的文件和 5,000 行的另一个文件,我需要一种高效的 Pythonic+Pnadas 方式

You can concat the dataframes , then check if all the elements are duplicated or not , then drop_duplicates and keep just the first occurrence:您可以连接数据帧,然后检查所有元素是否duplicated ,然后drop_duplicates并仅保留第一次出现:

m = pd.concat((df1,df2))
m[m.duplicated('id',keep=False)].drop_duplicates()

  id  value
0  a      2
2  c     22

你可以试试这个:

df = df1[df1.set_index(['id']).index.isin(df2.set_index(['id']).index)]
import pandas as pd

data1={'id':['a','b','c','d'],
       'value':[2,3,22,5]}

data2={'id':['c','a'],
       'value':[22,2]}

df1=pd.DataFrame(data1)
df2=pd.DataFrame(data2)
finaldf=pd.concat([df1,df2],ignore_index=True)

Output after concat连接后输出

   id   value
0   a   2
1   b   3
2   c   22
3   d   5
4   c   22
5   a   2

Final Ouput最终输出

finaldf.drop_duplicates()

    id  value
0   a   2
1   b   3
2   c   22
3   d   5

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

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