When concatenating pandas data frames using pd.concat
, the order of columns may change arbitrarily.
>>> df = pd.DataFrame([[1, 2], [3, 4]], index=[0, 1], columns=['B', 'A'])
>>> df2 = pd.DataFrame([[1, 2], [3, 4]], index=[0, 1], columns=['A', 'B'])
>>> pd.concat([df, df2])
A B #B A is good according to df!
0 2 1
1 4 3
0 1 2
1 3 4
To keep the column order unchanged, use the DataFrame.append
method.
>>> df.append(df2)[df.columns.tolist()]
B A
0 1 2
1 3 4
0 2 1
1 4 3
Now you can combine the data frames while keeping the order of the df
columns. If you want to combine multiple data frames, just pass a list of data frames such as df.append ([df1, df2])
.
However, DataFrame.append
is slow and should not be used when joining many rows.
I forgot to write the execution environment, so I added it
Recommended Posts