I was trying to calculate the frequency of disease_present (yes
) when smoking status is y
(yes) for each group (A, B, C, D)
The formula is = Number of samples with disease_present (yes) / No of samples with smoking status 'y' * 100
df
Sample_code Group smoking disease_present
1 A y yes
2 B n
3 C n yes
4 D y
5 B n yes
6 A y yes
7 C n
8 D y
9 A n yes
10 B y
11 D n yes
12 C n
13 A y yes
14 B y yes
I tried this
df.groupby("Group").count()
This gave the same number in every category
- Pandas by default turns empty cells into NaN
- I tried to replace NaN with empty strings with
df1 = df.fillna("")
. I still got the same results