简体   繁体   中英

Pandas dataframe raises KeyError when sort_values() method is called

I'm new at pandas and trying to simply learn something about it and it's dataframes. I want to sort data using one column, but when I tried to use:

 print(df.sort_values(by = 'avg_low'))

and

print(df.sort_values('avg_low'))

but it always throws KeyError. ('avg_low' is column's name) My data is:

month  avg_high  avg_low  record_high  record_low  avg_precipitation
Jan        58       42           74          22               2.97
Feb        34       42           74          22               2.97
Mar        54       42           74          22               1.97
Apr        65       42           74          21               2.97
May        32       42           74          22               3.32

what can I do to sort this somehow?

所以你在列中有空格,让我们使用str.strip清除它然后我们可以使用排序值

df.columns=df.columns.str.strip()

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