user20821492
user20821492

Reputation:

create a list with pairs from a column in pandas

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?

Upvotes: 1

Views: 112

Answers (1)

apan
apan

Reputation: 461

try this

pairs = []

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

print(pairs)

Upvotes: 1

Related Questions