Get keys of pandas.Series.value_counts

Viewed 12080

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?

3 Answers
for key, value in data.gender.value_counts().to_dict().items():
    print(key, value)
labels = data.gender.keys()
# or labels = data.gender.index.tolist()
sizes = data.gender.values
Related