Concatenate pandas dataframes with different column names

Asked

Viewed 4,440 times

0

I have two pandas Dataframes that I would like to match into one. They have the same number of columns in the same order, but have headers with different names. Is there any approach to efficiently combine these dataframes?

df1
index  Data1   Nome1
0      1-1-18  1
1      2-1-18  2

df2
index  Data2   Nome2
0      3-1-18  3
1      4-1-18  4

df_concat
index  Data   Nome
0      1-1-18  1
1      2-1-18  2
2      3-1-18  3
3      4-1-18  4

The only approach I’ve found so far was renaming the headers and then using the Concat method:

pd.concat([df1, df2, axis = 0, ignore_index = True)

However, I would like to know if there is a more general approach.

1 answer

2


I don’t think there’s anything more general, but I’d love to find an answer to that.

Looking at the Pandas Handbook on merging, Both the concat as to the append create a copy.

It is possible to use the trick of

df1=df1.append(df2) 

But he will create an entire copy in memory to make the append, just like the concat.

In the Only one question about copying, where the possibility of using is placed pd.merge(df1, df2, copy=False) (or df1.merge(df2, copy=False)) when df1.dtype != df2.dtype, but I couldn’t resolve your system with merge as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.