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