简体   繁体   中英

KeyError in Pandas sort_values

I am trying to sort a dataframe by a particular column: "Lat". However, although when I print out the column names, "Lat" clearly shows up, when I try to use it as the "by" parameter in the sort_values function, I get a KeyError. It doesn't matter which column name I use, I get a key error no matter what.

I have tried using different columns, running in place, stripping the columns names, nothing seems to work

print(lights_df.columns.tolist())
lights_by_lat = lights_df.sort_values(axis = 'columns', by = "Lat", kind 
= "mergesort")

outputs:

['the_geom', 'OBJECTID', 'TYPE', 'Lat', 'Long']

KeyError: 'Lat'

^output from trying to sort

All you have to do is remove the axis argument:

lights_by_lat = lights_df.sort_values(by = "Lat", kind = "mergesort")

and you should be good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM