0

I have a dataframe with multiple columns. I want to groupby column A (which is a person's name). Then I want to count total number of rows in column C grouped by column A. I also want to count number of unique rows in Column B grouped by column A.

Is there a way to do this in Python?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
PineNuts0
  • 4,740
  • 21
  • 67
  • 112
  • Yes there is. What have you tried? For example, are you familiar with `GroupBy.agg()`? Are you aware of agg funcs `count` and `nunique`? (or `len` instead of `count` if you want to include missing values) – wjandrea Aug 05 '23 at 02:12

1 Answers1

1

This:

df.groupby('A').agg({'C':'size', 'B':'nunique'})

although really, number of rows in C should just be the same with number of rows in B. This should also work

df.groupby('A')['B'].agg(['size','nunique'])
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74