I ended up using something similar to Stefan’s answer but slightly reformulated it to avoid having to use
apply. This looks a bit cleaner to me.
idx1 = df[df['animal'] == 'python'].iloc[:2].index idx2 = df[df['animal'] == 'python'].iloc[2:].index df.loc[idx1, "animal"] = "python_1" df.loc[idx2, "animal"] = "python_2" df.groupby("animal").sum()
CLICK HERE to find out more related problems solutions.