-1

I have a data frame that is set up as follows:

index   Category      Item
1       a             true
2       b             false
3       c             true
4       b             true
5       c             false
6       a             false
7       b             true
8       c             False

I want to make a grouped bar plot where I can visualize the percentage of true and false values for each category a, b, and c. I have provided my attempt at plotting this data below. The error I keep getting is "Could not interpret input 'percent' ". Any help is appreciated. I am extremely new to seaborn and pandas.

sns.barplot(data=DF.loc[:,["Category", "Item"]], x="Category", y="percent", hue="Item", order=["a", "b", "c"])
mozway
  • 194,879
  • 13
  • 39
  • 75

2 Answers2

0

The parameter y should be a column of the dataframe you pass to the function through the data parameter. Here you are passing it DF.loc[:,["Category", "Item"]] which has only 2 columns, Category and Item. Try to use data=DF instead.

Simone
  • 695
  • 4
  • 6
0

You first need to rework your dataframe to apply value_counts with normalized counts:

df2 = (df.groupby('Category')['Item']
         .value_counts(normalize=True)
         .rename('percent').reset_index()
        )

sns.barplot(data=df2, x="Category", y="percent", hue="Item", order=["a", "b", "c"])

NB. normalized counts are fractions (between 0 and 1), if you want percents, multiply by 100

bar plot

mozway
  • 194,879
  • 13
  • 39
  • 75