0

Is there a way in Pandas to groupby() and count() without naming a specific column? So typically in a data frame (df) with Columns A-D I could do

df.groupby(["A","B"]).count()

which will give me a two rows (C,D) with the count of non-empty (non-Nan) values of C, D where A and B have the same value. That is all nice, but oftentimes I'm just interested how many rows there are with the same A and B combination independent of what C and D are called at the moment and what their values are.

I can also just pick one of the columns and just get one column with the counts

df.groupby(["A","B"])["c"].count()

But for that I need to ensure that C is always there and is named "C". Sure I could include a dummy column

df.assign(dummy=1).groupby(["A","B"])["dummy"].count()

but I'm wondering if there is not more strait forward way.

Magellan88
  • 2,543
  • 3
  • 24
  • 36

0 Answers0