根据日期加入熊猫数据框

时间:2019-11-18 22:25:21

标签: python pandas

我有两个数据帧df1和df2

df1:

Player_A    Player_B    Date
  Sam         Casey     10/4
  Steve       Sam       11/4
  Casey       Steve     12/4

df2:

Player    Date    Count
 Sam      10/4      5
 Casey    10/4      3
 Steve    11/4      4
 Sam      11/4      4
 Casey    12/4      4
 Steve    12/4      5

我的目标是在df1中添加两个新列,即“ Count_A”和“ Count_B”,以从df2中拉出每个玩家的人数

所以我的输出是这样:

Player_A    Player_B    Date    Count_A     Count_B
  Sam         Casey     10/4      5            3
  Steve       Sam       11/4      4            4
  Casey       Steve     12/4      4            5

我正在尝试合并和联接,但不能完全正确地获取参数

感谢您的帮助

3 个答案:

答案 0 :(得分:2)

使用mergemap

df3 = df2.merge(df1, on='Date')
df1['Count_A'] = df1.Player_A.map(df3[df3.Player_A == df3.Player].set_index('Player_A')['Count'])
df1['Count_B'] = df1.Player_B.map(df3[df3.Player_B == df3.Player].set_index('Player_B')['Count'])

Out[697]:
  Player_A Player_B  Date  Count_A  Count_B
0      Sam    Casey  10/4        5        3
1    Steve      Sam  11/4        4        4
2    Casey    Steve  12/4        4        5

答案 1 :(得分:0)

一种替代方法是使用df.apply

import pandas as pd

df1['Count_A'] = df1.apply(lambda x: df2.Count[(df2.Player==x.Player_A) & (df2.Date==x.Date)].reset_index(drop=True), axis=1)[0].tolist()
df1['Count_B'] = df1.apply(lambda x: df2.Count[(df2.Player==x.Player_B) & (df2.Date==x.Date)].reset_index(drop=True), axis=1)[0].tolist()
df1

答案 2 :(得分:0)

要针对N种类型的玩家(不仅限于A和B)做到这一点,我们可以使用Dataframe.melt

df1.columns=df1.columns.str.replace('Player','')
new_df= ( df1.melt('Date',var_name='Type_Player',value_name='Player')
             .merge(df2,on=['Date','Player'],how='left')
             .set_index(['Date','Type_Player'])
             .unstack('Type_Player')
             .reset_index() )
new_df.columns=[x+y for x,y in new_df.columns]
print(new_df)

   Date Player_A Player_B  Count_A  Count_B
0  10/4      Sam    Casey        5        3
1  11/4    Steve      Sam        4        4
2  12/4    Casey    Steve        4        5