What is the Difference in Size and Count in pandas python

0 votes
I am not understanding at all how it differs?
Apr 30, 2018 in Data Analytics by CodingByHeart77
• 3,740 points
2,476 views

1 answer to this question.

0 votes

The major difference is "size" includes NaN values, the "count" does not include NaN. The below example will help you to understand:

df = pd.Dataframe( {"Name":["Nitin", "Sanju", "Manisha", "Manisha", "Sanju" , "Manisha"],

 "City":["Bombay", "Bombay", "Delhi", "Bombay", "Bombay", "Delhi"],

 "Val": [1, 2, 2, np.nan, np.nan, 3]})

df output:
# Output: 
#        City     Name  Val
# 0     Bombay  Nitin   1.0
# 1     Bombay  Sanju   2.0
# 2     Delhi   Manisha 2.0
# 3     Bombay  Manisha NaN
# 4     Bombay  Sanju   NaN
# 5     Delhi   Manisha 3.0
df.groupby(["Name", "City"])['Val'].size().reset_index(name='Size')
# Output: 
#       Name      City  Size
# 0    Nitin   Bombay   1
# 1    Sanju   Bombay   2
# 2    Manisha Delhi    2
# 3    Manisha Delhi    1
df.groupby(["Name", "City"])['Val'].count().reset_index(name='Count')

# Output:

#       Name      City  Size
# 0    Nitin   Bombay   1
# 1    Sanju   Bombay   1
# 2    Manisha Delhi    2
# 3    Manisha Delhi    0

I hope this example works for you.

answered Apr 30, 2018 by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by Gitika

Related Questions In Data Analytics

0 votes
1 answer
0 votes
1 answer

What is the difference between library () and require () functions in R ?

 library() require() Library () function gives an error message ...READ MORE

answered Sep 5, 2018 in Data Analytics by zombie
• 3,790 points
2,320 views
0 votes
2 answers

What is the difference between %% and % in R programming?

HI, %% returns remainder in case of numeric ...READ MORE

answered Aug 26, 2019 in Data Analytics by anonymous
• 33,030 points
1,946 views
0 votes
1 answer

What is the difference between list and vector in R?

The difference are - A list holds different ...READ MORE

answered Oct 29, 2019 in Data Analytics by Cherukuri
• 33,030 points
45,716 views
0 votes
2 answers

Replacing a row in pandas data.frame

key error. I love python READ MORE

answered Feb 18, 2019 in Data Analytics by anonymous
12,944 views
0 votes
1 answer

Converting a pandas data-frame to a dictionary

Emp_dict=Employee.to_dict('records') You can directly use the 'to_dict()' function ...READ MORE

answered May 23, 2018 in Data Analytics by Bharani
• 4,660 points
4,313 views
0 votes
1 answer

Adding values of common columns in pandas dataframe

Result = Set1.add(Set2)   x   y 0  70  ...READ MORE

answered May 30, 2018 in Data Analytics by Bharani
• 4,660 points
887 views
0 votes
1 answer

Pandas df header on 2 row

By default when you import a file, ...READ MORE

answered Apr 4, 2019 in Python by Yogi
27,502 views
0 votes
1 answer

How to rename columns in pandas (Python)?

You can use the rename function in ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by MD 1,627 views
0 votes
7 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP