Home > Enterprise >  Calculate percentage of occurences of a value in a dataframe column based on another column value
Calculate percentage of occurences of a value in a dataframe column based on another column value

Time:09-29

My dataframe looks something like below :

enter image description here

I need to calculate percentage of "contestants" (one of the value in Result column) that are still "operating" (one of the values in OperatingState column).

How can derive this in pandas ?

Update: Adding dtypes for columns

Startup           object
Product           object
Funding           object
Event             object
Result            object
OperatingState    object
dtype: object

CodePudding user response:

Try this:

perc = (((df['Result'] == 'Contestant') & (df['OperatingState'] == 'Operating')).sum() / df.shape[0]) * 100

Tested on example:

import pandas as pd

df = pd.DataFrame({'Result': ['Contestant', 's', 'Contestant', 's'], 'OperatingState': ['Operating', 'p', 'Operating', 'Operating']})

perc = (((df['Result'] == 'Contestant') & (df['OperatingState'] == 'Operating')).sum() / df.shape[0]) * 100
print(perc)

Output:

50.0
  • Related