Home > Enterprise >  Count the frequency of a df in percent
Count the frequency of a df in percent

Time:09-16

So I have a pandas dataframe

d = {'col1': [1, 2, 1, 6, 6, 1, 1, 2, 2, 2]}
dataframe = pd.DataFrame(data=d)

It is a quite large dataframe. What is the best way to output the frequency in percentage with print?

For example:

1: 40 %
2: 40 %
6: 20 %

CodePudding user response:

df.groupby('col1').apply(lambda x: x.value_counts()/df['col1'].count() * 100)

CodePudding user response:

You may try this :

>>> d = d.groupby('col1')['col1'].count().rename("perc").transform(lambda x: x/x.sum()) * 100
>>> d.reset_index()
    col1    perc
0   1       40.0
1   2       40.0
2   6       20.0
  • Related