通过索引搜索数据帧并导出信息

问题描述 投票:0回答:1

所以,我在range数据框中有索引。我想使用它们在测试数据框中找到值并将值从中提取到新数据框中。我当前的代码是:

d = []
for index in _range_.index:
    d.append((test.loc[[index],:])) 
_range_ data set:
              a
2334   0.097946
3345   0.098201
3357   0.091249
3486   0.098214
5862   0.097946
6873   0.098201
6885   0.091249
7014   0.098214


_test_ data set:
            0         1         2         3         4         5         
0      4.187268  4.261664  4.329495  4.458864  3.071192  3.652938    

python pandas dataframe
1个回答
0
投票

您可以使用'inner'在共同索引上将两个数据框一起join,然后仅保留test列。

cols = __test__.columns

df = __range__.join(__test__, how='inner')

df=df[cols]

不过,我无法在您的示例中测试此代码,可能值得在以后的文章https://stackoverflow.com/help/minimal-reproducible-example中阅读此代码>

© www.soinside.com 2019 - 2024. All rights reserved.