Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between sum() and count() in pandas?

Can you help me understand the difference between the statements mentioned below? Given that Survived column contains binary data (0,1), they give different answers:

df_train[df_train.Sex == 'female'].Survived.count()
df_train[df_train.Sex == 'female'].Survived.sum()
like image 241
NEX Avatar asked Sep 15 '25 09:09

NEX


2 Answers

sum() is for like 1+0 = 1. if data is 3 and 3 then it will return 6.

count() return number of rows, so it will return 2.

like image 183
Dhruv Raval Avatar answered Sep 17 '25 22:09

Dhruv Raval


count() will just count number of rows.

sum() will sum the 1's and 0's.

like image 43
Blakey Avatar answered Sep 18 '25 00:09

Blakey