0

I am trying to sort some data using Python/Pandas by year and month for a line plot.

My code is:

df.groupby(df['EXTRACT_DATE'].dt.strftime('%Y-%m'))['data'].sum().sort_values(ascending=True)

Which is returning:

enter image description here

Which is clearly not in order, but I'm not sure why and my data is like this:

Sort

2 Answers 2

3

Change to sort_index, after groupby your EXTRACT_DATE became index , so when you do sort_values , it sort the data not the %Y-%m

df.groupby(df['EXTRACT_DATE'].dt.strftime('%Y-%m'))['data'].sum().sort_index()
Sign up to request clarification or add additional context in comments.

Comments

1

You're using .sort_values() which sorts by the value (the sum in this case) and not by the index (the date). What you want to use instead is .sort_index().

1 Comment

Yes of course, thank you! Wood for the trees with this one!

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.