Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用多个数据帧中的列名查找另一个数据帧_Python_Pandas - Fatal编程技术网

Python 如何使用多个数据帧中的列名查找另一个数据帧

Python 如何使用多个数据帧中的列名查找另一个数据帧,python,pandas,Python,Pandas,我有三个不同的数据帧: 贝塔重量: +----------+-----+----------+--------+ | 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

我有三个不同的数据帧:

贝塔重量:

+----------+-----+----------+--------+
| 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    |
+----------+-----+----------+--------+
贝塔矩阵:

+---+-----+----------+--------+
|   | 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_矩阵作为查找更新Beta_权重,并使用Dyna检查此人是“N”、“S”还是“a”

生成的Beta_权重应如下所示:

+----------+-----+----------+--------+
| 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)

在OP的示例中,可以将
Dyna
Beta_矩阵
连接起来,并将
Beta_权重
一起替换。不过我想,他的实际任务可能不那么简单——可能是
Beta_权重
中的行,这些行在
Beta_矩阵
中找不到,从而阻止它被替换