简体   繁体   English

iterrows() 循环只读取最后一个值,只修改第一行

[英]iterrows() loop is only reading last value and only modifying first row

I have a dataframe test .我有一个 dataframe test My goal is to search in the column t1 for specific strings, and if it matches exactly a specific string, put that string in the next column over called t1_selected .我的目标是在t1列中搜索特定字符串,如果它与特定字符串完全匹配,则将该字符串放在名为t1_selected的下一列中。 Only thing is, I can't get iterrows() to go over the entire dataframe, and to report results in respective rows.唯一的问题是,我无法在整个 dataframe 上将iterrows()传递给 go,并在相应的行中报告结果。

for index, row in test.iterrows():
    if any(['ABCD_T1w_MPR_vNav_passive' in row['t1']]):
        #x = ast.literal_eval(row['t1'])
        test.loc[i, 't1_selected'] = str(['ABCD_T1w_MPR_vNav_passive'])

I am only trying to get ABCD_T1w_MPR_vNav_passive to be in the 4th row under the t1_selected , while all the other rows will have not found .我只是想让ABCD_T1w_MPR_vNav_passive位于t1_selected下的第 4 行,而所有其他行都not found The first entry in t1_selected is from the last row under t1 which I didn't include in the screenshot because the dataframe has over 200 rows. t1_selected中的第一个条目来自t1下的最后一行,我没有包括在屏幕截图中,因为 dataframe 有超过 200 行。

I tried to initialize an empty list to append output of我试图将一个空列表初始化为 append output

import ast
x = ast.literal_eval(row['t1'])

to see if I can put x in there, but the same issue occurred.看看我是否可以将x放在那里,但发生了同样的问题。

Is there anything I am missing?有什么我想念的吗? 在此处输入图像描述

for index, row in test.iterrows():
    if any(['ABCD_T1w_MPR_vNav_passive' in row['t1']]):
        #x = ast.literal_eval(row['t1'])
        test.loc[index, 't1_selected'] = str(['ABCD_T1w_MPR_vNav_passive'])

Where index is the row its written to.其中index是其写入的行。 With i it was not changing有了i ,它没有改变

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

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