簡體   English   中英

使用來自另一個具有條件的數據幀的值更新熊貓數據幀列

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

我有一個包含每周比賽對決的數據框和一個包含最終得分的第二個數據框。 我想用第二個團隊的得分更新第一個,條件是比賽在第 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

我嘗試重命名列以匹配並使用.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              

這給出了我希望的結果,但實際上不起作用。 當我嘗試再次重命名以更新 score2 時,我看到它正在使用 df2 中的第一行進行更新,而不是將它們與 df1 中的值進行匹配。 我嘗試過合並,但它會創建新列而不是更新現有列。 我計划向 df1 添加新游戲並每周執行此更新,我想更新而不是創建新列。 有什么方法可以做到這一點?

這個例子我想要的輸出是:

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              

讓我們試試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)

輸出:

   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              

你可以用合並來做到這一點:

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)

輸出:

    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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM