149

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 times the same row appears in the dataframe.

Group           Size      Time
Short          Small        2
Moderate       Medium       1 
Moderate       Small        1
Tall           Large        1
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
emax
  • 6,965
  • 19
  • 74
  • 141
  • 2
    Note on performance, including alternatives: [Pandas groupby.size vs series.value_counts vs collections.Counter with multiple series](https://stackoverflow.com/questions/50328246/pandas-groupby-size-vs-series-value-counts-vs-collections-counter-with-multiple) – jpp Jun 25 '18 at 14:02

3 Answers3

204

You can use groupby's size

import pandas as pd

# read the sample data from the OP
df =  pd.read_html('https://stackoverflow.com/q/33271098/7758804')[0]

Option 1:

dfg = df.groupby(by=["Group", "Size"]).size()

# which results in a pandas.core.series.Series
Group     Size
Moderate  Medium    1
          Small     1
Short     Small     2
Tall      Large     1
dtype: int64

Option 2:

dfg = df.groupby(by=["Group", "Size"]).size().reset_index(name="Time")

# which results in a pandas.core.frame.DataFrame
      Group    Size  Time
0  Moderate  Medium     1
1  Moderate   Small     1
2     Short   Small     2
3      Tall   Large     1

Option 3:

dfg = df.groupby(by=["Group", "Size"], as_index=False).size()

# which results in a pandas.core.frame.DataFrame
      Group    Size  Time
0  Moderate  Medium     1
1  Moderate   Small     1
2     Short   Small     2
3      Tall   Large     1
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
93

Update after pandas 1.1 value_counts now accept multiple columns

df.value_counts(["Group", "Size"])

You can also try pd.crosstab()

Group           Size

Short          Small
Short          Small
Moderate       Medium
Moderate       Small
Tall           Large

pd.crosstab(df.Group,df.Size)


Size      Large  Medium  Small
Group                         
Moderate      0       1      1
Short         0       0      2
Tall          1       0      0

EDIT: In order to get your out put

pd.crosstab(df.Group,df.Size).replace(0,np.nan).\
     stack().reset_index().rename(columns={0:'Time'})
Out[591]: 
      Group    Size  Time
0  Moderate  Medium   1.0
1  Moderate   Small   1.0
2     Short   Small   2.0
3      Tall   Large   1.0
BENY
  • 317,841
  • 20
  • 164
  • 234
5

Other posibbility is using .pivot_table() and aggfunc='size'

df_solution = df.pivot_table(index=['Group','Size'], aggfunc='size')
asantz96
  • 611
  • 5
  • 15