1

For example, if my table is something like:

df = pd.DataFrame({'user_id': ['abc', 'abc', 'def'],
                   'product': ['A1', 'A2', 'A3']})

How can I return a bar chart where the dimension X is product and the metric y is the count of the distinct values of user_id?

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Jurij
  • 33
  • 5

1 Answers1

0

You can groupby "product", count the unique values of "user_id" and plot:

(df.groupby('product')
   ['user_id'].nunique()
   .plot.bar()
)

output:

stacked bar plot

I find it a better example the other way around:

(df.groupby('user_id')
   ['product'].nunique()
   .plot.bar()
)

bar plot

You might also be interested in identifying the groups (again reversed example as more demonstrative):

(df.groupby('user_id')
   ['product'].value_counts()
   .unstack()
   .plot.bar(stacked=True)
)

stacked bar plot

mozway
  • 194,879
  • 13
  • 39
  • 75