2

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

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.