简体   繁体   English

如何将基于列的 dataframe 中的值添加到基于行的另一个 dataframe 中?

[英]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.

相关问题 我如何根据列单元格值和 append 查找一个 dataframe 上的一行到另一个 dataframe 上的一行? - How do i lookup a row on one dataframe based on the column cell value and append that to a row on another dataframe? 基于另一个 dataframe 的行值对一个 dataframe 中的列求和 - Sum column in one dataframe based on row value of another dataframe 如何将一列添加到基于另一个列值的数据框中? - How can I add a column to a dataframe that is based on another columns value? 在 Python 中,如何根据另一列更改 dataframe 的一列? - In Python, how do I change one column of a dataframe based on another? 如何根据另一个 dataframe 的匹配为 dataframe 的新列添加值? - how to add value to a new column to a dataframe based on the match of another dataframe? 如何根据键列将新行从 dataframe 添加到另一行 - How can I add new rows from a dataframe to another one based on key column Pandas DataFrame:为什么我不能通过行迭代基于另一列的值来更改一列的值? - Pandas DataFrame: Why I can't change the value of one column based on value of another through row iteration? 如何使用基于另一个DataFrame的列将一个DataFrame列转移到真值表? - How do I pivot one DataFrame column to a truth table with columns based on another DataFrame? 如何根据 Pandas 数据框中的另一列值添加列? - How to add column based on another column value in Pandas dataframe? Pandas基于连接将列从一个数据帧添加到另一个数据帧 - Pandas add column from one dataframe to another based on a join
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM