Table of Contents

Order columns alphabetically

scope | tabular data
tags | csv, reorder

Task

Convert

    name  address  number
     Ane      USA    1212
   Joane  England    2323

to

   address   name  number
       USA    Ane    1212
   England  Joane    2323

solution

df = df[sorted(df.columns)]

Example

$ ipython
Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]
In [1]:
import pandas as pd
df = pd.DataFrame(
  [['Ane', 'USA', 1212],
   ['Joane', 'England', 2323]],
  columns=['name', 'address', 'number'])
df
Out[1]:
    name  address  number
0    Ane      USA    1212
1  Joane  England    2323

In [2]:
df = df[sorted(df.columns)]

In [3]:
df
Out[3]:
   address   name  number
0      USA    Ane    1212
1  England  Joane    2323

See also