9

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?

4
  • 2
    df.columns=df.columns.str.strip(), then do sort Commented Dec 28, 2018 at 21:06
  • 2
    @ernest_k Oh, I pasted it from .txt document here and just forgot to remove commas.. Commented Dec 28, 2018 at 21:06
  • 1
    @W-B Thanks! It Works! post it as an answer Commented Dec 28, 2018 at 21:08
  • 1
    OK will do :-)~ Commented Dec 28, 2018 at 21:09

1 Answer 1

8

So you have white space in columns , let using str.strip clear it up then we can using sort values

df.columns=df.columns.str.strip()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.