根据2个DataFrame中的2个匹配列,在第二个DataFrame添加一列



我有2个DataFrame,我想在第二个DataFrame中添加一列,该列基于两个DataFrame的多个(在我的情况下是2(匹配列我试过下面的代码,但没有得到正确的答案。有人能帮我吗?


result = result.merge(out[out['COL1.']!=''].drop(['month'], axis=1), on=['COL1.'], how='left').merge(out[out['month']!=''].drop(['COL1.'], axis=1), on=['month'], how='left')

df1:

COL1.       count   month
1   Plassen     1293    4
2   Adamstuen   567     4
3   AHO.        1799    5
4   Akersgata   2418    4

df2:

station     month
1   Plassen     4
2   Adamstuen   4
3   AHO.        5
4   Akersgata.  6

我想要的是:

station     month.  count 
1   Plassen     4.       1293
2   Adamstuen   4.       567
3   AHO.        5.       1799

使用merge()方法并将drop()方法链接到它:

result=df2.merge(df1,right_on=['COL1.','month'],left_on=['station','month']).drop(columns=['COL1.'])

现在,如果你打印result,你会得到你想要的输出:

station     month   count
0   Plassen     4       1293
1   Adamstuen   4       567
2   AHO.        5       1799

在这种情况下,df.join()方法优于df.merge()方法

df2.rename(columns={'station':'COL1.'},inplace=True)
df1.set_index(['COL1.','month'],inplace=True)
df2.set_index(['COL1.','month'],inplace=True)
df2.join(df1)