简体   繁体   English

Pandas sort_values 中的 KeyError

[英]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.但是,虽然当我打印出列名时,“Lat”清楚地显示出来,但当我尝试将它用作 sort_values 函数中的“by”参数时,我得到了一个 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'] ['the_geom', 'OBJECTID', 'TYPE', 'Lat', 'Long']

KeyError: 'Lat' KeyError: '纬度'

^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.你应该很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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