Issue
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?
Solution
You can groupby
"product", count the unique values of "user_id" and plot:
(df.groupby('product')
['user_id'].nunique()
.plot.bar()
)
output:
I find it a better example the other way around:
(df.groupby('user_id')
['product'].nunique()
.plot.bar()
)
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)
)
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.