使用一个DataFrame中的值来保留另一个DataFrame中的值

时间:2017-12-13 20:23:49

标签: python pandas dataframe

我创建了两个数据框,让我们调用它们,df1 df2其中

print(df1)
                a                  b
1        0.375241                1.0
2             NaN                2.0
3        0.448792                3.0
4             NaN                4.0

df1 = df1[np.isfinite(df1['a'])]
print(df1)
                a                  b
1        0.375241                1.0
3        0.448792                3.0


print(df2)
          aa           bb
1   0.047606          1.0
2   0.202927          1.0
3   0.205663          1.0
4        NaN          1.0
5   1.388080          1.0
6   0.097084          1.0
7   0.136873          1.0 
8        NaN          1.0
9        NaN          1.0
10       NaN          1.0
12  0.084676          2.0
13  0.236850          2.0
14  0.532835          2.0
15       NaN          2.0
16       NaN          2.0
17  0.035106          2.0
18       NaN          2.0
19       NaN          2.0
20       NaN          2.0
22  0.419956          3.0
23  0.267132          3.0
24  0.944217          3.0
25  0.403024          3.0
26       NaN          3.0
27  0.184425          3.0
28  0.473998          3.0
29       NaN          3.0
30       NaN          3.0
31       NaN          3.0
33  0.465454          4.0
34  0.240867          4.0
35       NaN          4.0
36       NaN          4.0
37  0.323195          4.0
38  0.193764          4.0
39       NaN          4.0
40       NaN          4.0
41       NaN          4.0
42       NaN          4.0

基于df1['b']的结果,其中我只有1& 3现在作为有效数字,我将如何保持df2['bb'] 1& 3,并将df2中的所有其他值设置为np.nan,以便这将是最终产品

df2 = df2[np.isfinite(df2['aa'])]
print(df2)
          aa           bb
1   0.047606          1.0
2   0.202927          1.0
3   0.205663          1.0
5   1.388080          1.0
6   0.097084          1.0
7   0.136873          1.0 
22  0.419956          3.0
23  0.267132          3.0
24  0.944217          3.0
25  0.403024          3.0
27  0.184425          3.0
28  0.473998          3.0

0 个答案:

没有答案