+3 votes
2 views
in Python by (1.2k points)
edited by

What exactly is the difference between groupby("x").count and groupby("x").size in Pandas?

1 Answer

+4 votes
by (11.9k points)

IN PANDAS

SIZE-

DataFrame.size

This function will return the size of DataFrame.

Example -

>>> s = pd.name({'s': 1, 'h': 2, 'i': 3, ā€˜vā€™:4}

>>> s.size

4

COUNT-

DataFrame.count

This function counts all the non- NA values of the DataFrame.

NA values are -  None, NaN, NaT.

EXAMPLE -

Constructing DataFrame from a dictionary:

>>> df = pd.DataFrame({"Name":

...                   ["shivangi", "sakshi", "aditi", "aditya", "vanshika"],

...                   "Sex": [F, np.nan, F, M, F],

...                   "Age": [21, 28, np.nan, 30, 46)

>>> df

  Name            Sex Age

0    shivangi      F 21

1    sakshi         NaN 28

2   aditi              F NaN

3    aditya          M 30

4    vanshika     F 46

The NA values will not be counted :

>>> df.count()

Name    5

Sex      4

Age      4

So, basically size will count all values including Nan, whereas count will count all values excluding Nan.

...