[英]Enumerate in 'for' function
I want to add a column named X
in every .tsv
file.我想在每个.tsv
文件中添加一个名为X
的列。 I want this column to have the value of the corresponding index in the folder_names
list (one value of the folder_names
per.tsv file).我希望此列具有folder_names
列表中相应索引的值( folder_names
per.tsv 文件的一个值)。 But the enumerate
function repeat itself in each iteration of the for loop
, so the column 'X' always get the last value of names
instead of the corresponding one.但是enumerate
function 在for loop
的每次迭代中重复自身,因此列“X”总是得到names
的最后一个值而不是对应的值。
I got these two lists:我得到了这两个列表:
all_files_tsv = [tsv_file_1, tsv_file_2.... tsv_file_n]
folder_names = [folder_name_1, folder_name_2.... folder_name_n]
And the output desired is the following:所需的 output 如下:
tsv_file_1
: tsv_file_1
:
Column1第 1 列 | Column2第 2 列 | X X |
---|---|---|
1 1 | A一个 | folder_name_1文件夹名称_1 |
2 2 | B乙 | folder_name_1文件夹名称_1 |
3 3 | C C | folder_name_1文件夹名称_1 |
tsv_file_2
: tsv_file_2
:
Column1第 1 列 | Column2第 2 列 | X X |
---|---|---|
1 1 | --- --- | folder_name_2文件夹名称_2 |
2 2 | --- --- | folder_name_2文件夹名称_2 |
3 3 | --- --- | folder_name_2文件夹名称_2 |
And this is the code that I have right now:这是我现在拥有的代码:
for file_ in all_files_tsv:
df = pd.read_csv(file_,sep = '\t', header=0)
for index, names in enumerate(folder_names):
df['X'] = names
Any idea of how could I solve this?知道我该如何解决这个问题吗?
You don't need enumerate()
.你不需要enumerate()
。 You can iterate over all_files_tsv
and folder_names
in parallel using zip()
to get corresponding elements.您可以使用zip()
并行迭代all_files_tsv
和folder_names
以获取相应的元素。
for file, name in zip(all_files_tsv, folder_names):
df = pd.read_csv(file, sep='\t', header=0)
df['X'] = name
df.to_csv(file, sep='\t', header=0)
Nested loops are used when you want a cross product between two lists, zip()
is used when you want to pair corresponding elements.当您想要两个列表之间的叉积时使用嵌套循环,当您想要配对相应的元素时使用zip()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.