Edward Edward - 3 months ago 22
Python Question

Column order in pandas.concat

I do as below:

data1 = pd.DataFrame({ 'b' : [1, 1, 1], 'a' : [2, 2, 2]})
data2 = pd.DataFrame({ 'b' : [1, 1, 1], 'a' : [2, 2, 2]})
frames = [data1, data2]
data = pd.concat(frames)
data


a b
0 2 1
1 2 1
2 2 1
0 2 1
1 2 1
2 2 1


The data column order is in alphabet order. Why is it so?
and how to keep the original order?

Answer

You are creating DataFrames out of dictionaries. Dictionaries are a unordered which means the keys do not have a specific order. So

d1 = {'key_a': 'val_a', 'key_b': 'val_b'}

and

d2 = {'key_b': 'val_b', 'key_a': 'val_a'}

are the same.

In addition to that I assume that pandas sorts the dictionary's keys descending by default (unfortunately I did not find any hint in the docs in order to prove that assumption) leading to the behavior you encountered.

So the basic motivation would be to resort / reorder the columns in your DataFrame. You can do this as follows:

import pandas as pd

data1 = pd.DataFrame({ 'b' : [1, 1, 1], 'a' : [2, 2, 2]})
data2 = pd.DataFrame({ 'b' : [1, 1, 1], 'a' : [2, 2, 2]})
frames = [data1, data2]
data = pd.concat(frames)

print(data)

cols = ['b' , 'a']
data = data[cols]

print(data)