Python Pandas - Concat dataframes with different columns ignoring column names

Viewed 50335

I have two pandas.DataFrames which I would like to combine into one. The dataframes have the same number of columns, in the same order, but have column headings in different languages. How can I efficiently combine these dataframes?

df_ger
index  Datum   Zahl1   Zahl2
0      1-1-17  1       2
1      2-1-17  3       4

df_uk
index  Date    No1     No2
0      1-1-17  5       6
1      2-1-17  7       8

desired output
index  Datum   Zahl1   Zahl2
0      1-1-17  1       2
1      2-1-17  3       4
2      1-1-17  5       6
3      2-1-17  7       8

The only approach I came up with so far is to rename the column headings and then use pd.concat([df_ger, df_uk], axis=0, ignore_index=True). However, I hope to find a more general approach.

4 Answers

You can concat the dataframe values:

df = pd.DataFrame(np.vstack([df1.values, df2.values]), columns=df1.columns)
# or
df = pd.DataFrame(np.concatenate([df1.values, df2.values], axis=0), columns=df1.columns)
print(df)

  index   Datum Zahl1 Zahl2
0     0  1-1-17     1     2
1     1  2-1-17     3     4
2     0  1-1-17     5     6
3     1  2-1-17     7     8

If you want to reindex the index column

df['index'] = range(len(df))
print(df)

   index   Datum Zahl1 Zahl2
0      0  1-1-17     1     2
1      1  2-1-17     3     4
2      2  1-1-17     5     6
3      3  2-1-17     7     8
Related