Home > Mobile >  Plot a bar chart with Seaborn library and group by function
Plot a bar chart with Seaborn library and group by function

Time:11-05

The requirement is to Plot a bar chart using a seaborn library, showing total sales (Customer_Value) by the Last_region. Here my code

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
customer = pd.read_csv('D:\PythonTraining\Customer.csv')

df = customer.groupby('Last_region')['Customer_Value'].sum()
df

sns.barplot(x = df.Last_region, y = df.Customer_Value)
plt.show

I get an error:

AttributeError: 'Series' object has no attribute 'Last_region'.

How can I correct it? I believed after groupby, the attribute cant be referenced.

CodePudding user response:

The issue is that Last_region becomes the index when you group on it. Also note that df here is most likely a Series, not a DataFrame, in which case Customer_Value would also not be a column.

  • Either use x=df.index and y=df.values

    sns.barplot(x=df.index, y=df.values)
    
  • Or use data=df.reset_index() (now it's guaranteed to be a DataFrame with those columns)

    sns.barplot(data=df.reset_index(), x='Last_region', y='Customer_Value')
    

Alternatively, as Asish commented, you can change df so that Last_region is not the index:

  • Either set as_index=False while grouping

    df = customer.groupby('Last_region', as_index=False)['Customer_Value'].sum()
    
  • Or reset_index() after grouping

    df = customer.groupby('Last_region')['Customer_Value'].sum().reset_index()
    
  • Related