2

What's the easiest way in Pandas to turn this

df = pd.DataFrame({'Class': [1, 2], 'Students': ['A,B,C,D', 'E,A,C']})
df

   Class Students
0      1  A,B,C,D
1      2    E,A,C

Into this?

paired match

2 Answers 2

4

Let's try combinations:

from functools import partial
from itertools import combinations

(df.set_index('Class')['Students']
   .str.split(',')
   .map(partial(combinations, r=2))
   .map(list)
   .explode()
   .reset_index())

   Class Students
0      1   (A, B)
1      1   (A, C)
2      1   (A, D)
3      1   (B, C)
4      1   (B, D)
5      1   (C, D)
6      2   (E, A)
7      2   (E, C)
8      2   (A, C)
3
  • 2
    Thank you, cs95 fro introducing me to functools partial. From your experience, does this work faster that creating a lambda? Aug 29, 2020 at 17:28
  • 1
    @ScottBoston Yes, there are two reasons - the first is, it is never slower than a lambda (and can be faster sometimes). The second is, it just looks so much cleaner, it's great. :D
    – cs95
    Aug 29, 2020 at 17:30
  • 1
    Great. I am going to add this to my toolbox. I appreciate your response. Be safe and stay healthy. Aug 29, 2020 at 17:31
4

This need multiple steps with pandas only , split + explode , then drop_duplicates

df.Student=df.Student.str.split(',')
df=df.explode('Student')
df=df.merge(df,on='Class')
df[['Student_x','Student_y']]=np.sort(df[['Student_x','Student_y']].values, axis=1)
df=df.query('Student_x!=Student_y').drop_duplicates(['Student_x','Student_y'])
df['Student']=df[['Student_x','Student_y']].agg(','.join,axis=1)
df
Out[100]: 
    Class Student_x Student_y Student
1       1         A         B     A,B
2       1         A         C     A,C
3       1         A         D     A,D
6       1         B         C     B,C
7       1         B         D     B,D
11      1         C         D     C,D
17      2         A         E     A,E
18      2         C         E     C,E

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.