通过pd.concat()组合两个数据帧将返回NA

时间:2016-11-18 14:00:04

标签: pandas dataframe concat

我有两个具有相同时间索引的数据帧,我想将它们结合起来。 这里的df 1:

        SIM          Sim
        2016         7.044070
        2017        -5.969345
        2018        -9.398478
        2019        10.780780
        2020         4.670440
        2021         0.755153
        2022         2.774908
        2023        -1.593711
        2024         3.680238
        2025         2.649697
        2026        -2.129628
        2027        -7.995690
        2028       -11.580384
        2029         3.329955
        2030        -7.350348
        2031       -18.700540
        2032        -7.567082
        2033         6.708158
        2034       -16.560767
        2035       -10.297144
        2036         6.358823
        2037        -3.799261
        2038       -17.014105
        2039        -2.428987
        2040         4.503636

df2如下所示:

                      8
        2016   9.011255
        2017  -8.570188
        2018  16.400602
        2019   7.656812
        2020   4.410551
        2021  -0.088575
        2022  17.015454
        2023   9.829078
        2024   5.102307
        2025  21.613413
        2026 -16.868674
        2027  24.853295
        2028  10.936787
        2029   3.733469
        2030   7.476336
        2031  14.418942
        2032 -11.040764
        2033  36.520934
        2034  -5.891520
        2035  16.501258
        2036  18.216938
        2037  -0.361039
        2038   6.417441
        2039  17.289889
        2040   8.844421

以下代码返回NA值

    pd.concat([df1, df2], axis = 1)

SIM                Sim         8
2016              NaN   9.011255
2017              NaN  -8.570188
2018              NaN  16.400602
2019              NaN   7.656812
2020              NaN   4.410551
2021              NaN  -0.088575
2022              NaN  17.015454
2023              NaN   9.829078
2024              NaN   5.102307
2025              NaN  21.613413
2026              NaN -16.868674
2027              NaN  24.853295
2028              NaN  10.936787
2029              NaN   3.733469
2030              NaN   7.476336
2031              NaN  14.418942
2032              NaN -11.040764
2033              NaN  36.520934
2034              NaN  -5.891520
2035              NaN  16.501258
2036              NaN  18.216938
2037              NaN  -0.361039
2038              NaN   6.417441
2039              NaN  17.289889
2040              NaN   8.844421

type(df1)和type(df2)都是Pandas Dataframes。 有谁知道如何连接这两个数据帧?

1 个答案:

答案 0 :(得分:4)

我认为您需要set_index,因为DataFrames似乎有不同的索引值,因此concat无法按indexes对齐数据并获取NAN }:

pd.concat([df1.set_index('SIM'), df2], axis = 1)

通过评论编辑:

Int64Index中需要DataFrames,所以请尝试:

df1.index = df1.index.astype(int)