简体   繁体   English

使用来自另一个具有条件的数据帧的值更新熊猫数据帧列

[英]update pandas dataframe column with value from another dataframe with condition

I have a dataframe with game matchups by week and a second dataframe with final scores.我有一个包含每周比赛对决的数据框和一个包含最终得分的第二个数据框。 I want to update the first with scores for each team from the second with condition that game was played in week 1.我想用第二个团队的得分更新第一个,条件是比赛在第 1 周进行。

    df1 = pd.DataFrame([[1,'aa','hh','',''],
                       [1,'bb','ii','',''],
                       [2,'cc','jj','',''],
                       [1,'dd','kk','',''],
                       [1,'ee','ll','',''],
                       [1,'ff','mm','',''],
                       [2,'gg','nn','','']], columns=['week','team1','team2','score1','score2'])
    df1
Out[3]: 
   week team1 team2 score1 score2
0     1    aa    hh              
1     1    bb    ii              
2     2    cc    jj              
3     1    dd    kk              
4     1    ee    ll              
5     1    ff    mm              
6     2    gg    nn      

    df2 = pd.DataFrame([[1,'aa', 24],
                       [1,'bb', 27],
                       [2,'cc', 20],
                       [1,'dd', 7],
                       [1,'ee', 9],
                       [1,'ff', 20],
                       [2,'gg', 0],
                       [1,'hh', 10],
                       [1,'ii', 3],
                       [2,'jj', 21],
                       [1,'kk', 20],
                       [1,'ll', 13],
                       [1,'mm', 19],
                       [2,'nn', 14]], columns=['week','team','score'])
df2
Out[5]: 
    week team  score
0      1   aa     24
1      1   bb     27
2      2   cc     20
3      1   dd      7
4      1   ee      9
5      1   ff     20
6      2   gg      0
7      1   hh     10
8      1   ii      3
9      2   jj     21
10     1   kk     20
11     1   ll     13
12     1   mm     19
13     2   nn     14

I tried renaming columns to match and used .update我尝试重命名列以匹配并使用.update

df2.columns = ['week','team1','score1']
df1.update(df2.loc[(df2['week']== 1)])
df1
Out[7]: 
   week team1 team2 score1 score2
0   1.0    aa    hh     24       
1   1.0    bb    ii     27       
2   2.0    cc    jj              
3   1.0    dd    kk      7       
4   1.0    ee    ll      9       
5   1.0    ff    mm     20       
6   2.0    gg    nn              

which gives result I was hoping for but actually doesn't work.这给出了我希望的结果,但实际上不起作用。 When I try to rename again to update score2 I see that it is updating with the first rows from df2 instead of matching them with values in df1.当我尝试再次重命名以更新 score2 时,我看到它正在使用 df2 中的第一行进行更新,而不是将它们与 df1 中的值进行匹配。 I tried merge but it creates new columns rather than update existing ones.我尝试过合并,但它会创建新列而不是更新现有列。 I plan to add new games to df1 and perform this update weekly and I want to update rather than create new columns.我计划向 df1 添加新游戏并每周执行此更新,我想更新而不是创建新列。 What is a way to accomplish this?有什么方法可以做到这一点?

my desired output for this example is:这个例子我想要的输出是:

df1
Out[28]: 
   week team1 team2 score1 score2
0     1    aa    hh     24     10
1     1    bb    ii     27      3
2     2    cc    jj              
3     1    dd    kk      7     20
4     1    ee    ll      9     13
5     1    ff    mm     20     19
6     2    gg    nn              

Let's try map instead:让我们试试map

to_map = df2[df2.week==1].set_index('team')['score']

to_update = df1.week==1

df1.loc[to_update, 'score1'] = df1.loc[to_update,'team1'].map(to_map)
df1.loc[to_update, 'score2'] = df1.loc[to_update,'team2'].map(to_map)

Output:输出:

   week team1 team2 score1 score2
0     1    aa    hh     24     10
1     1    bb    ii     27      3
2     2    cc    jj              
3     1    dd    kk      7     20
4     1    ee    ll      9     13
5     1    ff    mm     20     19
6     2    gg    nn              

You can do it with merge:你可以用合并来做到这一点:

result=df1.merge(df2, left_on='team1', right_on='team').merge(df2, left_on='team2', right_on='team')[['week_x', 'team1', 'team2', 'score_x', 'score_y']]
result.columns=['week', 'team1', 'team2', 'score1', 'score2']
result.loc[result['week']!=1,['score1', 'score2']]=np.nan
print(result)

Output:输出:

    week team1 team2  score1  score2
0     1    aa    hh      24      10
1     1    bb    ii      27       3
2     2    cc    jj      20      21
3     1    dd    kk       7      20
4     1    ee    ll       9      13
5     1    ff    mm      20      19
6     2    gg    nn       0      14

暂无
暂无

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

相关问题 根据另一列中的条件从 Pandas 数据框中提取值 - Extract Value From Pandas Dataframe Based On Condition in Another Column Pandas:添加新列并按条件从另一个dataframe赋值 - Pandas: Add new column and assigning value from another dataframe by condition 熊猫列值从另一个数据框值更新 - pandas column value update from another dataframe value 如何查找两个 pandas dataframe 并从另一个 dataframe 列更新第一个 dataframe 列中的值? - how to do lookup on two pandas dataframe and update its value in first dataframe column from another dataframe column? 根据条件使用另一个数据帧列值更新一个数据帧值 - Update one dataframe value with another dataframe column value based on the condition 使用来自另一个数据帧的 if 条件在 Pandas 数据帧中创建一个新列 - create a new column in pandas dataframe using if condition from another dataframe 如何根据另一个 DataFrame 中的列更新 Pandas DataFrame 中的列 - How to update a column in pandas DataFrame based on column from another DataFrame Pandas数据框根据查询数据框中的值选择行,然后根据列值选择其他条件 - Pandas Dataframe Select rows based on values from a lookup dataframe and then another condition based on column value 从另一个数据帧中的列值替换pandas数据帧中的列中的值 - Replacing value in a column in pandas dataframe from a column value in another dataframe 从Pandas中不同数据框中的另一个匹配列更新数据框中的列值 - update a column value in a dataframe from another matching column in different dataframe in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM