Skip to content Skip to sidebar Skip to footer

Aggregating In Panda Dataframe

I have the below panda dataframe Now I want to sum the count for [user 1][user 2] and [user 2][user 1]. For example, the count value for [user 1 = 1][user 2 = 92] is count = 1 an

Solution 1:

A solution grouping sorted tuples:

df['users'] = list(zip(df.user1,df.user2))
df['users'] = df.users.apply(lambda t:tuple(sorted(t)))
counts = df.groupby('users', as_index=False)['count'].sum()
counts[['user1','user2']] = counts.users.apply(pd.Series)
counts = counts[['user1','user2','count']] # Select and reorder columns

Post a Comment for "Aggregating In Panda Dataframe"