Home > database >  Concat pandas dataframes
Concat pandas dataframes

Time:08-31

How to concat without column names?

>> df = pd.DataFrame({'col1': [1], 'col2': [4]})
>> df1 = pd.DataFrame([[5,5]])

>> pd.concat([df, df1])

    col1    col2    0       1
0   1.0     4.0     NaN     NaN
0   NaN     NaN     5.0     5.0

Also the types changed into float64 from int64 if you see closely.

Expected

    col1    col2
0   1        4
0   5        5

CodePudding user response:

Create same columns names in both DataFrames by DataFrame.set_axis:

df1 = df1.set_axis(df.columns, axis=1)

Or assign columns names:

df1.columns = df.columns
#alternative - got 0,1 columns
#df.columns = df1.columns

Last use concat:

out = pd.concat([df, df1], ignore_index=True)

CodePudding user response:

Temporarily set_axis with that of df on df1:

pd.concat([df, df1.set_axis(df.columns, axis=1)], ignore_index=True)

NB. append is being deprecated, don't use it.

output:

   col1  col2
0     1     4
1     5     5
  • Related