Search code examples
pythonpandasmatplotlibplotpandas-groupby

Plotting groupby() in python


I cannot find a way to plot the grouped data from the follwoing data frame:

                Processed Card       Transaction ID  Transaction amount     Error_Occured  
Date                                                                   
2019-01-01           Carte Rouge    217142203412           147924.21       0
2019-01-01              ChinaPay    149207925233            65301.63       1 
2019-01-01            Masterkard    766507067450           487356.91       5
2019-01-01                  VIZA    145484139636            97774.52       1
2019-01-02           Carte Rouge    510466748547           320951.10       3

I want to create a plot where: x-axis: Date, y-axis: Errors_Occured, points/lines colored by Processed Card. I tried grouping the data frame first and ploting it using pandas plot:

df = df.groupby(['Date','Processed Card']).sum('Error_Occured')
df = df.reset_index()
df.set_index("Date",inplace=True)
df.plot(legend=True)
plt.show()

But I get the plot where Transaction ID is displayed and not the cards: SEE THE PLOT


Solution

  • You can do something like this:

    fig, ax = plt.subplots()
    
    df = pd.DataFrame()
    df['Date'] = ['2019-01-01','2019-01-01','2019-01-01','2019-01-01','2019-01-02']
    df['Card'] = ['Carte Rouge', 'ChinaPay', 'Masterkard', 'VIZA', 'Carte Rouge']
    df['Error_Occured'] = [0,1,5,1,3]
    
    series = dict(list(df.groupby(['Card'])))
    
    for name, s in series.items():
        ax.plot(s['Date'], s['Error_Occured'], marker='o', label=name)
        
    plt.legend()
    plt.show()
    

    This produces the following with the data provided:

    enter image description here

    Note that you only want to group by card, not date.