Python: get a frequency count based on two columns (variables) in pandas dataframe some row appers Python: get a frequency count based on two columns (variables) in pandas dataframe some row appers python python

Python: get a frequency count based on two columns (variables) in pandas dataframe some row appers


You can use groupby's size:

In [11]: df.groupby(["Group", "Size"]).size()Out[11]:Group     SizeModerate  Medium    1          Small     1Short     Small     2Tall      Large     1dtype: int64In [12]: df.groupby(["Group", "Size"]).size().reset_index(name="Time")Out[12]:      Group    Size  Time0  Moderate  Medium     11  Moderate   Small     12     Short   Small     23      Tall   Large     1


Update after pandas 1.1 value_counts now accept multiple columns

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

You can also try pd.crosstab()

Group           SizeShort          SmallShort          SmallModerate       MediumModerate       SmallTall           Largepd.crosstab(df.Group,df.Size)Size      Large  Medium  SmallGroup                         Moderate      0       1      1Short         0       0      2Tall          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  Time0  Moderate  Medium   1.01  Moderate   Small   1.02     Short   Small   2.03      Tall   Large   1.0


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

df_solution = df.pivot_table(index=['Group','Size'], aggfunc='size')