Below is a small sample of a dataframe I have, and I want to add a calculated row to the bottom of it:
sch q1 q2 q3
acc Yes Yes No
acc Yes No No
acc Yes No No
acc Yes Yes Yes
I want to add a row at the bottom that will give me the percentage of values that are 'Yes' for each column, so that it would look like below.
sch q1 q2 q3
acc Yes Yes No
acc Yes No No
acc Yes No No
acc Yes Yes Yes
acc 1.00 0.5 0.25
Any help would be greatly appreciated.
I see your lambda and raise a pure pandas solution:
df.append(df.eq('Yes').mean(), ignore_index=True)
You don't specify what should happen to the sch column, so I ignored it. In my current solution this column will get the value 0.
assume the following approach:
In [11]: df.loc[len(df)] = ['acc'] + df.filter(regex='^q\d+') \
.eq('Yes').mean().values.tolist()
In [12]: df
Out[12]:
sch q1 q2 q3
0 acc Yes Yes No
1 acc Yes No No
2 acc Yes No No
3 acc Yes Yes Yes
4 acc 1 0.5 0.25
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With