Python Forum

Full Version: Does the order of columns in the DataFrame matter?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Thanks. I tried
A_new = A.reindex(['Ohio', 'Colorado'], axis=1, level=1)
but it returned:

IndexError: Too many levels: Index has only 1 level, not 2
Can't completely reproduce the problem. Below is working example:

import pandas as pd
import numpy as np
df = pd.DataFrame({'key1': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'], 
                            'key2': [2000, 2001, 2002, 2001, 2002], 
                           'data': np.arange(5.)})  

gg = df.set_index(['key1', 'key2']).unstack('key1')
Output:
data key1 Nevada Ohio key2 2000 NaN 0.0 2001 3.0 1.0 2002 4.0 2.0
gg.reindex([['data', 'data'],['Ohio', 'Nevada']], axis='columns')
Output:
data key1 Ohio Nevada key2 2000 0.0 NaN 2001 1.0 3.0 2002 2.0 4.0
This works for pandas v. 1.0.1. Behavior can depend on Pandas version.
Pages: 1 2