I was experimenting with the kaggle.com Titanic data set (data on every person on the Titanic) and came up with a gender breakdown like this:
gender = df.sex.value_counts()
gender
male 577
female 314
I would like to find out the percentage of each gender on the Titanic.
My approach is slightly less than ideal:
from __future__ import division
pcts = gender / gender.sum()
pcts
male 0.647587
female 0.352413
Is there a better (more idiomatic) way?
Thanks!
This function is implemented in pandas, actually even in value_counts(). No need to calculate :)
just type:
which gives exactly the desired output.
Please note that value_counts() excludes NA values, so numbers might not add up to 1. See here: http://pandas-docs.github.io/pandas-docs-travis/generated/pandas.Series.value_counts.html (A column of a DataFrame is a Series)
I know it is an old post, but I hope this answer's gonna help someone in the future. In case you wish to show percentage one of the things that you might do is use
value_counts(normalize=True)
as answered above by @fanfabbb.With that said, for many purposes, you might want to show it in the percentage out of a hundred. that can be achieved like so
In this case, we multiply the results by hundred, round it to one decimal point and add the percentage sign.
Hope it could help:)
If you want to merge counts with percentage, can use:
I think I would probably do this in one go (without importing division):
or perhaps, remembering you want a percentage:
Much of a muchness really, your way looks fine too.