python - Uppercase precedence in pandas column name assignment -
is there reason why column name assignment in pandas favours uppercase on lower case?
example:
dframe = dataframe({'city':['alma','brian head', 'fox park'], 'altitude':[3158,3000,2762]})
returns dataframe columns in order altitude, city.
whereas:
dframe = dataframe({'city':['alma','brian head', 'fox park'], 'altitude':[3158,3000,2762]})
returns dataframe columns in order city,altitude.
is pandas specific or general python behaviour?
you didn't ask this, i'm assuming there implied question how preserve original ordering? if so, here 3 ways:
1) same basic dictionary constructor, wrap in collections.ordereddict
(thanks @shx2 correction):
from collections import ordereddict df1 = pd.dataframe( ordereddict([ ('city',['alma','brian head', 'fox park']), ('altitude',[3158,3000,2762]) ]))
2) non-dictionary constructor specifiy data array , column names separately, however, requires row-centric entry rather column-centric dictionary constructor:
lst = [['alma','brian head','fox park'], [3158,3000,2762]] df2 = pd.dataframe( map(list, zip(*lst)), columns = ['city','altitude'] )
3) simplest way specify order after create dataframe (thanks @edchum catching error in original post):
df3 = df[['city','altitude']]
test results same three:
in [149]: all(df1==df2) out[149]: true in [150]: all(df1==df3) out[150]: true
Comments
Post a Comment