简体   繁体   English

我如何根据列单元格值和 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?

I have been searching for this answer for a day now.我一直在寻找这个答案一天。 I can't find a solution.我找不到解决方案。 I have two data frames for NBA stats.我有两个用于 NBA 统计数据的数据框。 One has just team names and the other has team names and stats associated.一个只有团队名称,另一个有团队名称和相关的统计数据。 I want to look up each team name on the first dataframe and append the stats data from the second one to the rows that match that team name.我想在第一个 dataframe 和 append 上查找每个团队名称,从第二个到与该团队名称匹配的行的统计数据。

df1 = 'DATE' : [rows of dates], 'TEAM_NAME': [row of team names with duplicates]
df2 = 'TEAM_NAME': [row of unique team names], 'STAT #1' ['row of stats], etc....

I want df1 to look something like this:我希望df1看起来像这样:

'DATE'   'TEAM_NAME'  'STAT 1' 'STAT 2' etc...
1-Jan-21  Boston        23        15    
5-Jan-21  Detroit       45        90
1-Jan-21  Boston        23        15

UPDATE* I used merge and join and it does do exactly what i need, there is one problem.更新* 我使用了合并和加入,它确实做了我需要的,有一个问题。 Both functions group my data by team names as it encounters them to join.这两个函数在遇到加入时按团队名称对我的数据进行分组。 I need it to retain the original order and replace the rows that way.我需要它来保留原始顺序并以这种方式替换行。

Instead of individually pulling the values from the cells, just merge the two data frames.无需单独从单元格中提取值,只需合并两个数据框即可。

merged = df1.merge(df2,how='left')

Because both df1 and df2 have 'TEAM_NAME' columns, the merge operation will automatically merge on those values, assuming those are the only shared columns between the two dataframes.因为 df1 和 df2 都有 'TEAM_NAME' 列,所以合并操作将自动合并这些值,假设这些是两个数据帧之间唯一的共享列。 If the frames have more than one shared column, specify the join columns;如果框架有多个共享列,则指定连接列;

merged = df1.merge(df2,how='left',on='TEAM_NAME')

And if you need to merge on both date and team_name columns;如果您需要合并 date 和 team_name 列;

merged=df1.merge(df2,how='left',on=['TEAM_NAME','DATE'])

but to answer your original question, cells can be retrieved by value like so:但要回答您的原始问题,可以按如下值检索单元格:

stat1 = df2.loc[df2.TEAM_NAME=='LAKERS','STAT#1']

暂无
暂无

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

相关问题 如何将基于列的 dataframe 中的值添加到基于行的另一个 dataframe 中? - How do I add the value from one dataframe based on a column to another dataframe based on a row? 基于另一个 dataframe 的行值对一个 dataframe 中的列求和 - Sum column in one dataframe based on row value of another dataframe 如何在具有另一列最大值的行中的一个 dataframe 列中找到值? - How do I find the value in one dataframe column in the row with the maximum value of another column? Pandas Dataframe:对于给定的行,尝试基于在另一列中查找值来分配特定列中的值 - Pandas Dataframe: for a given row, trying to assign value in a certain column based on a lookup of a value in another column 如何使用 Python 查看数据框中一列的值并根据这些结果在另一列中附加一个新值? - How do I use Python to look at the values of one column in a dataframe and append a new value in another column based on those results? Pandas DataFrame:为什么我不能通过行迭代基于另一列的值来更改一列的值? - Pandas DataFrame: Why I can't change the value of one column based on value of another through row iteration? 如何根据数据帧的行值是子字符串/包含在另一个数据帧的行值中连接两个数据帧? - How do I join two dataframes based on a dataframe's row value being a substring/contained in another dataframe's row value? 如何根据行中的另一个值在 dataframe 中创建列(Python) - How to create a column in a dataframe based on another value in the row (Python) 如何将一行附加到另一个数据帧 - How to append a row to another dataframe 如何根据另一行更新数据框中的列值? - How do you update a column's value in a dataframe based off of another row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM