create a list with pairs from a column in pandas

Question:

I have a column with pairs like these

id pairs
1 a,b,c
2 b,d
3 a
4 d,e
5 g,h
6 a,h
7 f,d
8 o,p

I want to have these output

[('ca', 'b'), ('a', 'c'),('b','c'),('b','d'),('d','e'),('g','h'),('a','h'),('f','d'),('o','p')]

I did these, but not the desired solution

for pair in combinations([df['pairs']], 2):
print(pairs)

any suggestions?

Asked By: Jimmy

||

Answers:

try this

pairs = []

for row in df['pairs']:
    row_list = row.split(',')
    for pair in combinations(row_list, 2):
        pairs.append(pair)

print(pairs)
Answered By: apan
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.