Reputation: 793
I have two dataframes with the same time index, which I would like to combine. Here's 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 Looks as follows:
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
The following code Returns NA values
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) and type(df2) are both Pandas Dataframes. Does anyone know how to connect the two dataframes?
Upvotes: 1
Views: 974
Reputation: 862701
I think you need set_index
, because it seems there are different index values in both DataFrames
, so concat
cannot align data by indexes
and get NAN
:
pd.concat([df1.set_index('SIM'), df2], axis = 1)
EDIT by comment:
You need Int64Index
in both DataFrames
, so try:
df1.index = df1.index.astype(int)
Upvotes: 4