Explore Courses Blog Tutorials Interview Questions
0 votes
in Data Science by (17.6k points)

Hello I have the following dataframe.

    Group           Size

    Short          Small

    Short          Small

    Moderate       Medium

    Moderate       Small

    Tall           Large

I want to count the frequency of how many time the same row appears in the dataframe.

    Group           Size      Time

    Short          Small        2

    Moderate       Medium       1 

    Moderate       Small        1

    Tall           Large        1

1 Answer

0 votes
by (41.4k points)

Using groupby's size:

In [23]: df.groupby(["Group", "Size"]).size()


Group     Size

Moderate  Medium 1

          Small     1

Short     Small 2

Tall      Large 1

dtype: int64

In [24]: df.groupby(["Group", "Size"]).size().reset_index(name="Time")


      Group    Size Time

0  Moderate  Medium 1

1  Moderate   Small 1

2     Short   Small   2

3      Tall Large     1

Browse Categories