簡體   English   中英

根據另一個表的列更新表信息

[英]update table information based on columns of another table

我是 python 的新手,有兩個數據框,df1 包含有關所有學生及其組和分數的信息,df2 包含有關少數學生更改組和分數時的更新信息。 如何根據 df2 的值(組和分數)更新 df1 中的信息?

df1

   +----+----------+-----------+----------------+
    |    |student No|   group   |       score    |
    |----+----------+-----------+----------------|
    |  0 |        0 |         0 |       0.839626 |
    |  1 |        1 |         0 |       0.845435 |
    |  2 |        2 |         3 |       0.830778 |
    |  3 |        3 |         2 |       0.831565 |
    |  4 |        4 |         3 |       0.823569 |
    |  5 |        5 |         0 |       0.808109 |
    |  6 |        6 |         4 |       0.831645 |
    |  7 |        7 |         1 |       0.851048 |
    |  8 |        8 |         3 |       0.843209 |
    |  9 |        9 |         4 |       0.84902  |
    | 10 |       10 |         0 |       0.835143 |
    | 11 |       11 |         4 |       0.843228 |
    | 12 |       12 |         2 |       0.826949 |
    | 13 |       13 |         0 |       0.84196  |
    | 14 |       14 |         1 |       0.821634 |
    | 15 |       15 |         3 |       0.840702 |
    | 16 |       16 |         0 |       0.828994 |
    | 17 |       17 |         2 |       0.843043 |
    | 18 |       18 |         4 |       0.809093 |
    | 19 |       19 |         1 |       0.85426  |
    +----+----------+-----------+----------------+

df2
+----+-----------+----------+----------------+
|    |   group   |student No|       score    |
|----+-----------+----------+----------------|
|  0 |         2 |        1 |       0.887435 |
|  1 |         0 |       19 |       0.81214  |
|  2 |         3 |       17 |       0.899041 |
|  3 |         0 |        8 |       0.853333 |
|  4 |         4 |        9 |       0.88512  |
+----+-----------+----------+----------------+

結果

自由度:3

   +----+----------+-----------+----------------+
    |    |student No|   group   |       score    |
    |----+----------+-----------+----------------|
    |  0 |        0 |         0 |       0.839626 |
    |  1 |        1 |         2 |       0.887435 |
    |  2 |        2 |         3 |       0.830778 |
    |  3 |        3 |         2 |       0.831565 |
    |  4 |        4 |         3 |       0.823569 |
    |  5 |        5 |         0 |       0.808109 |
    |  6 |        6 |         4 |       0.831645 |
    |  7 |        7 |         1 |       0.851048 |
    |  8 |        8 |         0 |       0.853333 |
    |  9 |        9 |         4 |       0.88512  |
    | 10 |       10 |         0 |       0.835143 |
    | 11 |       11 |         4 |       0.843228 |
    | 12 |       12 |         2 |       0.826949 |
    | 13 |       13 |         0 |       0.84196  |
    | 14 |       14 |         1 |       0.821634 |
    | 15 |       15 |         3 |       0.840702 |
    | 16 |       16 |         0 |       0.828994 |
    | 17 |       17 |         3 |       0.899041 |
    | 18 |       18 |         4 |       0.809093 |
    | 19 |       19 |         0 |       0.81214  |
    +----+----------+-----------+----------------+

我的代碼從 df2 更新 df1

dfupdated = df1.merge(df2, how='left', on=['student No'], suffixes=('', '_new'))
dfupdated['group'] = np.where(pd.notnull(dfupdated['group_new']), dfupdated['group_new'],
                                         dfupdated['group'])
dfupdated['score'] = np.where(pd.notnull(dfupdated['score_new']), dfupdated['score_new'],
                                         dfupdated['score'])
dfupdated.drop(['group_new', 'score_new'],axis=1, inplace=True)
dfupdated.reset_index(drop=True, inplace=True)

但我面臨以下錯誤

KeyError: "['group'] not in index"

我不知道怎么了

嘗試:

dfupdated = df1.merge(df2, on='student No', how='left')
dfupdated['group'] = dfupdated['group_y'].fillna(dfupdated['group_x'])
dfupdated['score'] = dfupdated['score_y'].fillna(dfupdated['score_x'])
dfupdated.drop(['group_x', 'group_y','score_x', 'score_y'], axis=1,inplace=True)

會給你你想要的解決方案。

從每個組中獲得最大值

dfupdated.groupby(['group'], sort=False)['score'].max()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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