Home > Mobile >  Concatenate dataframes without doubling columns number
Concatenate dataframes without doubling columns number

Time:11-16

I am trying to concatenate DF1:

   datecreated     1     2     3     4     5  ...   331   332   333   334   335   336
0  2022-11-14      4000  3900  3850  3810  3790  ...  5520  5300  5180  4990  4730  4520

with DF2:

   datecreated     1       2       3  ...     333     334     335     336
0  2022-11-15      4000    3200    3150  ...  5544    4190    4735    3520.0

Using:

pd.concat([df_, df2.reset_index()], axis=0)

I get:

          datecreated     1       2       3    ...     
0         2022-11-14      4000.0  3900.0  3850.0  ...     
0         2022-11-15      NaN     NaN     NaN  ...  

There are some problems: the columns have doubled after concat (lots of NaNs) and the rows index is not incremental. How can I fix that?

CodePudding user response:

I don't appear to be able to reproduce your issue... but to give a guess, I'd bet columns names are integers in one, and strings in the other:

Given:

# df1
   datecreated     1     2     3     4     5
0  2022-11-14      4000  3900  3850  3810  3790

# df2
   datecreated     1       2       3
0  2022-11-15      4000    3200    3150

Doing:

# Possible Fix:
df1.columns, df2.columns = [[str(x) for x in df.columns] for df in [df1, df2]]

# To re-increment the index, use ignore_index=True:
df = pd.concat([df1, df2], ignore_index=True)
print(df)

Output:

  datecreated     1     2     3       4       5
0  2022-11-14  4000  3900  3850  3810.0  3790.0
1  2022-11-15  4000  3200  3150     NaN     NaN
  • Related