A pandas dataframe has 2 columns.
first filter only first values by GroupBy.transform and GroupBy.first comparing by original values in boolean indexing and then count values by GroupBy.size:
GroupBy.transform
GroupBy.first
boolean indexing
GroupBy.size
df = df[df.groupby('Number')['Character'].transform('first').eq(df['Character'])] df = df.groupby(['Number','Character']).size().reset_index(name='count') print (df) Number Character count 0 111 a 2 1 222 b 2 2 333 c 3
2.1m questions
2.1m answers
63 comments
56.6k users