I’m trying to add more rows or records to my data frame, let’s say it looks like this:
ID age 44 23 31 25
and I have a CSV file stored in another data frame without headers
33 55 22 23 29 22
now I want a new data frame that looks like this
ID age 44 23 31 25 33 55 22 23 29 22
I have tried using append and concat but I didn’t get the result that I wanted
Advertisement
Answer
Assuming df1
/df2
, you can use set_axis
to copy the axis of the first DataFrame, then concat
:
out = pd.concat([df1, df2.set_axis(df1.columns, axis=1)], ignore_index=True)
output:
ID age 0 44 23 1 31 25 2 33 55 3 22 23 4 29 22
NB. ignore_index=True
is optional, this is just to avoid having duplicated indices. Without it:
ID age 0 44 23 1 31 25 0 33 55 1 22 23 2 29 22