如何使用数据框合并,但仍包含NaN值?

时间:2020-10-05 16:59:19

标签: python pandas merge

我想要NaN值,但是合并后,我的一些值就会消失。

import pandas as pd

data1 = {'tic':['PYPL','PYPL','PYPL'],
        'fyear':['2017','2018','2019'],
        'asset':[40774,43332,51333],
        'debt':[0,0,5368],
        'ni':[1795,2057,2459],
       }
data2 = {'tic':['SQ','SQ','SQ'],
        'fyear':['2017','2018','2019'],
        'asset':[2187.27,3281.023,4551.258],
        'debt':[362.404,1051.141,1047.662],
        'ni':[-62.813,-38.453,375.446],
       }
data3 = {'tic':['GPN','GPN','GPN'],
        'fyear':['2017','2018','2019'],
        'asset':[12998.069,13230.774,44480.162],
        'debt':[4559.408,5015.168,9487.852],
        'ni':[468.425,452.053,430.613],
       }
data4 = {'fyear':['2015','2016','2017','2018','2019','2015','2016','2017','2018','2019','2015','2016'],
         'tic':['PYPL','PYPL','PYPL','PYPL','PYPL','SQ','SQ','SQ','SQ','SQ','GPN','GPN'],
         'price':[36.2,39.47,73.62,84.09,108.17,13.09,13.63,34.67,56.09,62.56,104.38,77.69],
    
}
PYPL = pd.DataFrame(data1)
SG = pd.DataFrame (data2)
gpn = pd.DataFrame (data3)
prc = pd.DataFrame (data4)

frame = pd.concat([PYPL,SG,gpn])

frame.merge(prc)

这给了我一个被截断的数据框,但是不幸的是,我试图将更多内容包括在内。

fyear stk_ticker price asset debt ni
0 2015 PYPL 36.20 NaN NaN NaN
1 2016 PYPL 39.47 NaN NaN NaN
2 2017 PYPL 73.62 40774.000 0.000 1795.000
3 2018 PYPL 84.09 43332.000 0.000 2057.000
4 2019 PYPL 108.17 51333.000 5368.000 2459.000
5 2015 SQ 13.09 NaN NaN NaN
6 2016 SQ 13.63 NaN NaN NaN
7 2017 SQ 34.67 2187.270 362.404 -62.813
8 2018 SQ 56.09 3281.023 1051.140 -38.453
9 2019 SQ 62.56 4551.258 1047.662 375.446
10 2014 GPN 104.38 NaN NaN NaN
11 2015 GPN 77.69 NaN NaN NaN
12 2016 GPN 69.41 NaN NaN NaN
13 2017 GPN 100.24 12998.069 4559.408 468.425
14 2018 GPN 103.13 13230.774 5015.168 452.053
15 2019 GPN 182.56 44480.162 9487.852 430.613

这就是我想要得到的。我不确定是否必须将它们串联或合并在一起?

1 个答案:

答案 0 :(得分:0)

尝试使用how='right'

df = frame.merge(prc, how='right')
print(df)

     tic fyear      asset      debt        ni   price
0   PYPL  2015        NaN       NaN       NaN   36.20
1   PYPL  2016        NaN       NaN       NaN   39.47
2   PYPL  2017  40774.000     0.000  1795.000   73.62
3   PYPL  2018  43332.000     0.000  2057.000   84.09
4   PYPL  2019  51333.000  5368.000  2459.000  108.17
5     SQ  2015        NaN       NaN       NaN   13.09
6     SQ  2016        NaN       NaN       NaN   13.63
7     SQ  2017   2187.270   362.404   -62.813   34.67
8     SQ  2018   3281.023  1051.141   -38.453   56.09
9     SQ  2019   4551.258  1047.662   375.446   62.56
10   GPN  2015        NaN       NaN       NaN  104.38
11   GPN  2016        NaN       NaN       NaN   77.69