展开snakemake中的多个pandas列以迭代行



我试图在同一个pandas数据框中使用2列作为基础,通过snakemake生成输出文件。

我的代码的一个最小的例子是这样的:
import pandas as pd
df = pd.DataFrame.from_dict({'col1':[1,2,3], 'col2':['A','B','C']})
rule all:
input:
expand('data/{c1}/{c2}.tsv', c1 = df['col1'], c2 = df['col2'])
rule make_files:
output:
"data/{c1}/{c2}.tsv"
shell:
"touch {output}"

我遇到的问题是,我想要的输出是3个文件,这将是逐行迭代的结果(即1/A)。tsv 2/B。tsv, 3/C.tsv),但是当前脚本给出的是9个文件,这些文件是我的行排列(即1/A. tsv)。tsv, 1/B。tsv, 1/C。tsv, 2/A。tsv 2/B。tsv, 2/C。tsv 3/。tsv 3/B。tsv 3/C.tsv)。

如有任何帮助,不胜感激

这是一个潜在的修复:

import pandas as pd
df = pd.DataFrame.from_dict({'col1':[1,2,3], 'col2':['A','B','C']})
foo = []
for index, row in df.iterrows():
foo.append('{}/{}'.format(row['col1'],row['col2']))
rule all:
input:
expand('data/{c1}.tsv', c1 = foo )
rule make_files:
output:
"data/{c1}.tsv"
shell:
"touch {output}"

这将为您提供所需的3个文件夹。实际上,您刚刚创建了一个字符串列表,其中包含您想要从您的两列中创建的组合。

最新更新