How to keep column order in case of concatenate pandas DataFrame

When I concatenate pandas DataFrame, column order was changed.
I want to keep original column order.

Pandas DataFrame is useful for data handling in Python.

But when we concatenate DataFrame, sometimes column order changes.

If column order is changed, it seems weird.

If possible, we want to use original column order.

So today I will introduce about "How to keep column order in case of concatenate pandas DataFrame".

TOC

Column order change issue

Column order change issue

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

If we want column order as A to B , it seems weird.

How to keep column order in case of concatenate pandas DataFrame

idea, 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

GitHub – 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.

Keep column order in case of concat DataFrame – Qiita

But it is not correct.
According to its comment, column list is used in the article.

Conclusion

Today I described about "How to keep column order in case of concatenate pandas DataFrame".

Important points are following.

If we want to keep column order likeA, B, we can use column list col_list.
We can set the list to DataFrame like df[col_list].

Even in concatenate case, we can specify column order.

If you like this article, please
Like or Follow !

If you like this article, please share !

Author

karasanのアバター karasan System engineer

Mid-career engineer (AI, Data science, Salesforce, etc.).
Good at Python and SQL.

TOC