How to calculate the percentage of the number of values in pandas?
I have a DataFrame
that contains various values.
import pandas as pd
df = pd.DataFrame({"data": [1, 1, 1, 1, 0, 0, 0, 2, 2, 3]})
I want to calculate how many percentages of the total data each value occupies, that is, to get a table of the form:
value | percent
_____________________
0 | 30 ( или 0.3)
1 | 40 ( или 0.4)
2 | 20 ( или 0.2)
3 | 10 ( или 0.1)
I can calculate it like this:
# Добавляю еще одну колонку, чтобы нормально посчитать count()
df['column'] = 1
df2 = df.groupby('data').count()
df2['percent'] = df2['column'] / len(df.index)
And I get what I'm looking for:
column percent
data
0 3 0.3
1 4 0.4
2 2 0.2
3 1 0.1
However, I still have the feeling that I'm doing everything wrong. And such issues should be solved much easier. Can you tell me how best to solve my problem?
2
1 answers
You can use the GroupBy.size() method-in this case, you do not need to create a new column:
In [4]: df.groupby('data').size() / len(df)
Out[4]:
data
0 0.3
1 0.4
2 0.2
3 0.1
dtype: float64
4
Author: MaxU, 2018-11-04 20:06:29