0

I wanted to count the total from NAME, B, C, D columns like so.

NAME B C D TOTAL
dorj jan hr nd 11
tash feb mkt nd 12
dorj jan fin yy 19

Column "Count" counts the values.

NAME B C D TOTAL COUNT
dorj jan hr nd 11 2
dorj jan fin yy 19 2
tash feb mkt kk 12 1
Tshering Tashi
  • 27
  • 1
  • 1
  • 6
  • [groupby count](https://pandas.pydata.org/docs/reference/api/pandas.core.groupby.GroupBy.count.html) -> `ed = df.groupby('NAME', as_index=False)['B'].count()` or if you want counts of distinct `NAME` `B` groups groupby multiple columns -> `ed = df.groupby(['NAME', 'B'], as_index=False)['B'].count()` – Henry Ecker Oct 18 '21 at 03:31
  • thank you, I wanted to add count column in the dataframe. – Tshering Tashi Oct 18 '21 at 04:01
  • [transform count](https://stackoverflow.com/a/29791952/15497888) -> `df['COUNT'] = df.groupby('NAME')['B'].transform('count')` or `df['COUNT'] = df.groupby(['NAME', 'B'])['B'].transform('count')`. It's still unclear whether we're grouping by `NAME` and `B` or just `NAME` – Henry Ecker Oct 18 '21 at 04:11

0 Answers0