简体   繁体   中英

How to use column names from multiple dataframes as look-up for another dataframe

I have three different pandas dataframes:

Beta_Weights:

+----------+-----+----------+--------+
| PersonID | HCW | Adaptive | Static |
+----------+-----+----------+--------+
|      111 | 0.3 | 0.3      | 0.3    |
|      112 | 0.3 | 0.3      | 0.3    |
|      113 | 0.3 | 0.3      | 0.3    |
+----------+-----+----------+--------+

Beta_Matrix:

+---+-----+----------+--------+
|   | HCW | Adaptive | Static |
+---+-----+----------+--------+
| N |   0 | 0.5      | 0.5    |
| S |   0 | 0.4      | 0.6    |
| A |   0 | 0.3      | 0.7    |
+---+-----+----------+--------+

(note that N, S and A are the indices of this dataframe)

and Dyna:

+----------+-----+
| PersonID | nsa |
+----------+-----+
|      111 | S   |
|      112 | A   |
|      113 | N   |
+----------+-----+

I'd like to update Beta_Weights using Beta_Matrix as a lookup and Dyna to check if the person is an "N", "S" or an "A".

The resulting Beta_Weights should look like this:

+----------+-----+----------+--------+
| PersonID | HCW | Adaptive | Static |
+----------+-----+----------+--------+
|      111 |   0 | 0.4      | 0.6    |
|      112 |   0 | 0.3      | 0.7    |
|      113 |   0 | 0.5      | 0.5    |
+----------+-----+----------+--------+

Using update

Beta_Matrix['PersonID']=Dyna.set_index('nsa')['PersonID'].reindex(Beta_Matrix.index)
# here is try to using one key map with another , then we can do merge or others later on

Beta_Weights.set_index('PersonID',inplace=True)
Beta_Weights.update(Beta_Matrix.set_index('PersonID'))
Beta_Weights.reset_index(inplace=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM