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,720 points
1,257 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 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,770 points
562 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
• 32,490 points
420 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 28, 2019 in Data Analytics by Cherukuri
• 32,490 points
11,019 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
7,276 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,580 points
3,294 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,580 points
273 views
0 votes
1 answer

Pandas df header on 2 row

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

answered Apr 3, 2019 in Python by Yogi
12,263 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 by MD 525 views
0 votes
7 answers