I use pandas.Series.value_counts
to count gender of users. But I also need to get the keys of a result to draw a plot and use keys as a label of the plot.
For example the result of data.gender.value_counts()
is:
female 6700
male 6194
brand 5942
unknown 1117
And I need to get a list ['female', 'male', 'brand', 'unknown']
as well and keep the order.
How can I do that?
Demo:
In [165]: s
Out[165]:
female 6700
male 6194
brand 5942
unknown 1117
Name: value_counts, dtype: int64
In [166]: s.index.tolist()
Out[166]: ['female', 'male', 'brand', 'unknown']
Plot:
In [169]: s.plot.bar(rot=0)
Out[169]: <matplotlib.axes._subplots.AxesSubplot at 0xd659da0>
Result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With