Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
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()

Out[23]:

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")

Out[24]:

      Group    Size Time

0  Moderate  Medium 1

1  Moderate   Small 1

2     Short   Small   2

3      Tall Large     1

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...