what should i do to group two columns interchangeably?

Example with shorter DataFrame:

df = pd.DataFrame({'name1': ['Alex', 'Alex', 'Sarah', 'Martin'], 'name2': ['Martin', 'Martin', 'Alex', 'Alex']})

df['tmp'] = df.apply(frozenset, axis=1)
df['count'] = df.groupby('tmp')['name1'].transform('size')
df = df.set_index('tmp')
df = df[~df.index.duplicated()].reset_index(drop=True)
print(df)

Prints:

   name1   name2  count
0   Alex  Martin      3
1  Sarah    Alex      1

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top