Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas add calculated row to bottom of dataframe

Tags:

python

pandas

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.

like image 506
Cameron Avatar asked Nov 26 '25 03:11

Cameron


2 Answers

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.

like image 144
Sebastiaan Avatar answered Nov 28 '25 15:11

Sebastiaan


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
like image 25
MaxU - stop WAR against UA Avatar answered Nov 28 '25 17:11

MaxU - stop WAR against UA