[英]How to use column names from multiple dataframes as look-up for another dataframe
我有三种不同的熊猫数据框:
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 |
+---+-----+----------+--------+
(请注意,N,S和A是此数据帧的索引)
和Dyna:
+----------+-----+
| PersonID | nsa |
+----------+-----+
| 111 | S |
| 112 | A |
| 113 | N |
+----------+-----+
我想使用Beta_Matrix作为查找和Dyna来更新Beta_Weights,以检查此人是“ N”,“ S”还是“ A”。
生成的Beta_Weights应该如下所示:
+----------+-----+----------+--------+
| PersonID | HCW | Adaptive | Static |
+----------+-----+----------+--------+
| 111 | 0 | 0.4 | 0.6 |
| 112 | 0 | 0.3 | 0.7 |
| 113 | 0 | 0.5 | 0.5 |
+----------+-----+----------+--------+
使用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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.