pythonpandas

Pandas - rearrange columns if column exists


I have a dataframes that are in jumbled order how can I rearrange the columns if they exits.

One      Two       Three  Six    Four     Five
1         2         3      6      4         5
1         2         3      6      4         5
...

How can I arrange this columns in order? The issue here is that all six column might not be there in all occasions. So I need a simple line that can arrange it in order of One Two Three Four Five Six if that column exits. I mean if Two is not in df then it should be One Three Four Five Six


Solution

  • You can change order by DataFrame.reindex and then remove only missing values columns:

    df1 = (df.reindex(['One', 'Two','Three','Four','Five','Six'], axis=1)
              .dropna(how='all', axis=1))
    print (df1)
       One  Three  Four  Five  Six
    0    1      3     4     5    6
    1    1      3     4     5    6
    

    Or is possible create order categoricals in columns and then sorting columns:

    c = ['One', 'Two','Three','Four','Five','Six']
    df.columns = pd.CategoricalIndex(df.columns, categories=c, ordered=True)
    df1 = df.sort_index(axis=1)
    print (df1)
       One  Three  Four  Five  Six
    0    1      3     4     5    6
    1    1      3     4     5    6