Reputation: 193
I have a df
I have to find all the combinations of Group (let's say 2 pairs) and then have to group them across unique ID's
Output:
Currently I found a way to generate all the combinations but cannot seem to group by unique ID's
I referred to below links as well: Pandas find all combinations of rows under a budget
Code to generate the pairs:
from itertools import combinations
li_4 =[]
for index in list(combinations(df.group.unique(),2)):
li_4.append([index[0],index[1]])
Upvotes: 2
Views: 68
Reputation: 323226
We can do merge
then np.sort
and pass the result to crosstab
after remove the duplicate with drop_duplicates
s = df.merge(df,on='Id')
s['New'] = list(map(lambda x : ''.join(x),np.sort(s[['Group_x','Group_y']].values,axis=1).tolist()))
s = s.drop_duplicates(['Id','New'])
s = pd.crosstab(s.Id,s.New)
s
Out[88]:
New aa ab ac ad af bb bc bd be bf cc cd dd de ee ff
Id
2 1 1 1 1 0 1 1 1 0 0 1 1 1 0 0 0
3 0 0 0 0 0 1 0 1 1 0 0 0 1 1 1 0
4 1 1 0 0 1 1 0 0 0 1 0 0 0 0 0 1
Upvotes: 2