简体   繁体   中英

Python Pandas - Don't sort bar graph on y axis values

I am beginner in Python. I have a Series with Date and count of some observation as below

Date      Count

2003        10

2005        50

2015        12

2004        12

2003        15

2008        10

2004        05

I wanted to plot a graph to find out the count against the year with a Bar graph (x axis as year and y axis being count) . I am using the below code

import pandas as pd

pd.value_counts(sfdf.Date_year).plot(kind='bar')

I am getting the bar graph which is automatically sorted on the count. So I am not able to clearly visualize how the count is distributed over the years. Is there any way we can stop sorting the data on the bar graph on the count and instead sort on the x axis values (i,e year)?

The following code uses groupby() to join the multiple instances of the same year together, and then calls sum() on the groupby() object to sum it up. By default groupby() pushes the grouped object to the dataframe index. I think that groupby() automatically sorts, but just in case, sort(axis=0) will sort the index. All that then remains is to plot. All in one line:

df = pd.DataFrame([[2003,10],[2005,50],[2015,12],[2004,12],[2003,15],[2008,10],[2004,5]],columns=['Date','Count'])
df.groupby('Date').sum().sort(axis=0).plot(kind='bar')

I know this is an old question, but in case someone is still looking for another answer.

I solved this by adding .sort_index(axis=0)

So, instead of this:

pd.value_counts(sfdf.Date_year).plot(kind='bar')

you can write this:

pd.value_counts(sfdf.Date_year).sort_index(axis=0).plot(kind='bar')

Hope, this helps.

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