Reputation: 93
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
Upvotes: 6
Views: 15013
Reputation: 52
I think it's because you are seeking on the wrong axis.
For example, this dataframe:
Let's say: I want to order by the row axis with label "1". So you put this:
df.sort_values(by='1',axis=0,inplace=False)
But the result give the error: KeyError: '1'
The solution is changing the axis to columns:
df.sort_values(by='1',axis=1,inplace=False)
I think it's because you want to order by row label "1" data, but the axis is "index", so ordering by "1" label is not possible because numbers are reordered in the column axis and not the index axis.
I hope this helps others.
Upvotes: 1
Reputation: 317
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.
Upvotes: 7