Python Pandas - Don't sort bar graph on y axis values
Question: 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)?


Answer: 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')
    

