Column order in pandas.concat

后端 未结 6 1656
既然无缘
既然无缘 2021-02-04 00:26

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         


        
6条回答
  •  野性不改
    2021-02-04 00:56

    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 (probably) 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)
    

提交回复
热议问题