简体   繁体   English

如何遍历数据框以打印两个列表列的所有可能组合?

[英]How do I iterate through a data frame to print all possible combinations of two list columns?

Lets say I have a datavframe:可以说我有一个datavframe:

Column1第 1 列 Column2第 2 列
1 1 A一个
2 2 A, B甲,乙
3 3 A, B, C A、B、C

I want to print every possible combination from the two lists on a new row.我想在新行上打印两个列表中的所有可能组合。 The output should look something like: output 应该类似于:

1A
2A 
2B
3A 
3B 
3C

Let's try split Column2 into list then explode Column2 .让我们尝试将Column2 split为列表,然后Column2 explode At last join two columns and convert to list.最后加入两列并转换为列表。

out = (df.assign(Column2=df['Column2'].str.split(', '))
       .explode('Column2')
       # [['Column1', 'Column2']] # uncomment this line if there are more than two target columns
       .astype(str)
       .agg(''.join, axis=1)
       .tolist())
print(out)

['1A', '2A', '2B', '3A', '3B', '3C']

Better use pure python here:最好在这里使用纯 python:

from itertools import product
out = [''.join(x) for a,b in zip(df['Column1'], df['Column2'])
       for x in product([str(a)], b.split(', '))]

output: ['1A', '2A', '2B', '3A', '3B', '3C'] output: ['1A', '2A', '2B', '3A', '3B', '3C']

Classical loop:经典循环:

for a,b in zip(df['Column1'], df['Column2']):
    for x in product([str(a)], b.split(', ')):
        print(''.join(x))

output: output:

1A
2A
2B
3A
3B
3C

Another possible solution:另一种可能的解决方案:

df.apply(lambda x: [str(x.Column1) + item for item in x.Column2.split(', ')], axis=1).explode()

Another way to use itertools.product :另一种使用itertools.product的方法:

from itertools import product
import re
out = df.apply(lambda x: list(
                product([str(x['Column1'])], list(re.sub('\s*,\s*', '', x['Column2'])))),
               axis=1)
out = [''.join(b) for a in out for b in a]

print(out):打印出):

['1A', '2A', '2B', '3A', '3B', '3C']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM