Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get keys of pandas.Series.value_counts

Tags:

python

pandas

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?

like image 662
rel1x Avatar asked Jun 04 '17 11:06

rel1x


1 Answers

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:

enter image description here

like image 193
MaxU - stop WAR against UA Avatar answered Sep 22 '22 00:09

MaxU - stop WAR against UA