[英]How do I add the value from one dataframe based on a column to another dataframe based on a row?
I have two dataframes that I am trying to combine, however, I have not been able to do the normal merge or appends because the join is based on a column for one and a row value for another.我有两个要组合的数据框,但是,我无法进行正常的合并或追加,因为连接基于一个列和另一个的行值。 I want to see where a cell value in a row matches the column value in another, and then add another value based on that comparison.我想查看一行中的单元格值与另一行中的列值匹配的位置,然后根据该比较添加另一个值。 Showing the dataframes should help.显示数据框应该会有所帮助。
df1: df1:
date division team opponent
04-03 E0 Man City Man Utd
05-03 E1 Reading Millwall
05-03 E2 Wycombe MK Dons
df2: df2:
date E0_avg_goals E1_avg_goals E2_avg_goals
04-03 1.9 2.1 1.1
05-03 1.68 2.2 1.3
06-03 1.7 1.9 1.25
end goal:最终目标:
date division team opponent league_avg_goals
04-03 E0 Man City Man Utd 1.9
05-03 E1 Reading Millwall 2.2
05-03 E2 Wycombe MK Dons 1.3
So I want to put the average league goals in for each row, based on which division the team in the 'team' column is in. But since in df1 the division is the column header, I have been unable to do this with normal joins and merges.因此,我想根据“团队”列中的团队所在的分区为每一行输入平均联赛进球数。但是由于在 df1 中分区是 header 列,因此我无法通过正常加入来做到这一点并合并。
Is there a merge or append that I am missing to solve this?是否有我缺少的合并或 append 来解决这个问题?
You could actually use a merge but after a bit of reshaping:您实际上可以使用合并,但经过一些重塑:
pd.merge(df1,df2.melt(id_vars='date').assign(division = lambda x: x['variable'].str[:2]), on=['division','date'])
prints:印刷:
date division team opponent variable value
0 04-Mar E0 Man City Man Utd E0_avg_goals 1.9
1 05-Mar E1 Reading Millwall E1_avg_goals 2.2
2 05-Mar E2 Wycombe MK Dons E2_avg_goals 1.3
Try this:尝试这个:
df2 = df2.set_index('date')
df2.columns = df2.columns.str.split('_', n=1, expand=True)
df2_map = df.stack(0).rename_axis(['date', 'division'])
df1.set_index(['date', 'division']).join(df2_map).reset_index()
Output: Output:
date division team opponent avg_goals
0 04-03 E0 Man City Man Utd 1.9
1 05-03 E1 Reading Millwall 2.2
2 05-03 E2 Wycombe MK Dons 1.3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.