-1

Is there an efficient way to group by and create a count col in a data frame containing a single column.

I tried but involves creating an extra col in original dataframe i.e. in df in this example.

item={'colx':['a','b','a','c']}

df=pd.DataFrame(item)
df
colx
0   a
1   b
2   a
3   c
df['row_count']=1
df.groupby(['colx'])['row_count'].sum().reset_index()
​
colx    row_count
0   a   2
1   b   1
2   c   1
Divyank
  • 811
  • 2
  • 10
  • 26
itthrill
  • 1,241
  • 2
  • 17
  • 36
  • I think you're looking for pd.Series.value_counts() https://pandas.pydata.org/docs/reference/api/pandas.Series.value_counts.html – Leo Aug 22 '22 at 14:57
  • that can be a workaround. But is there a more general groupby way to do this. – itthrill Aug 22 '22 at 14:59

1 Answers1

0
df.groupby('colx')['colx'].agg(row_count='size' ).reset_index()
    colx    row_count
0      a    2
1      b    1
2      c    1
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Naveed
  • 11,495
  • 2
  • 14
  • 21