We use pandas DataFrame in Python.
When we concatenate DataFrame, sometimes column order changes.
So this article introduce how to keep column order in case of concatenate DataFrame.
Column order change
Assuming that there are DataFrame df1
and df2
.
They have same columns but different order.
If we concatenate these DataFrames by pd.concat
, column order will be aligned to one of them.
Like example below, sometimes column order will be B to A
,not A to B
.
import pandas as pd df1 = pd.DataFrame([[2, 1], [4, 3]], columns=['B', 'A']) df2 = pd.DataFrame([[5, 6], [7, 8]], columns=['A', 'B']) print("df1") print(df1) # B A # 0 2 1 # 1 4 3 print("df2") print(df2) # A B # 0 5 6 # 1 7 8 df_concat = pd.concat([df1, df2], ignore_index=True, sort=False) print("df_concat") print(df_concat) # B A # 0 2 1 # 1 4 3 # 2 6 5 # 3 8 7
Solution
If we want A to B
, we can make ordered column list col_list
.
Then set it to DataFrame like df[col_list]
.
So we can keep column order.
df_concat2 = pd.concat([df1, df2], ignore_index=True, sort=False)[df2.columns.to_list()] print("df_concat2") print(df_concat2) # A B # 0 1 2 # 1 3 4 # 2 5 6 # 3 7 8
Sample code:
yKRSW/sample_df_concat_order: Sample of keeping column order of dataframe
The article below says following.
In order to keep column order, use
DataFrame.append
method.
But it is not correct.
As its comment, column list is used in the article.
Refference:
Keep column order in case of concat DataFrame - Qiita