首页 > 解决方案 > 循环遍历 pandas 数据框以使用 for 循环替换现有值

问题描述

问题:我正在尝试使用 for 循环逐行遍历数据帧。但它没有按预期工作。我知道有 iterrows() 和 itertuple() 我想试验 for 循环。

你能告诉我这是哪里出错了吗?

样本数据

data3 = {"one":['101', '102', '103' , '104'],
     "two":['101', '105', '106', '104'],
     "three": ['102', '5', '107', '108'],
     "other": ['101', '102', '103' , '104']
     }
df3 = pd.DataFrame(data3)

目标:检查每一行的“二”列,如果“一”列中存在“二”列的值,则创建一个值为“del”的新列“new_col”。如果“一”列中不存在该值,则将“new_col”创建为“保留”。例如,如果“二”列有 101,我想将它与“一”列的所有值进行比较

我的代码:

dfToList1 = df3['two'].tolist()
for x in dfToList1:
   if x in df3['one'].values:
       df3['new_col'] = 'del'
   else:
       df3['new_col'] = 'keep'

然后我可以用类似'none'的字符串替换'two'中与'one'匹配的值

df3.loc[df3['new_col'] == 'del', 'two'] = 'none'

我的输出:

理想情况下,在第 2 行和第 3 行,'two' 中的 5 和 107 不包含在'one' 中,因此第 2 行和第 3 行中的 new_col 应该具有值'keep',但我没有得到它。

    one other   three   two new_col
0   101 101     102     101     del
1   102 102       5     105     del
2   103 103     107     106     del
3   104 104     108     104     del

预期产出

    one other   three   two  new_col
0   101 101     102     101     del
1   102 102       5     105     keep
2   103 103     107     106     keep
3   104 104     108     104     del

标签: pythonpandasfor-loop

解决方案


使用np.whereSeries.eqSeries.isin检查。

df3['newcol']=np.where(~df3.two.isin(df3.one),'keep','del')

或按列“一”与第二列的任何共同值进行选择:

df3['newcol']=np.where(~df3.one.isin(df3.loc[df3.two.eq(df3.one),'two']),'keep','del')
print(df3)

   one  two three other newcol
0  101  101   102   101    del
1  102  105     5   102   keep
2  103  106   107   103   keep
3  104  104   108   104    del

细节

two_coincident_one=df3.loc[df3.two.eq(df3.one),'two']
print(two_coincident_one)
0    101
3    104
Name: two, dtype: object


~df3.one.isin(two_coincident_one)

0    False
1     True
2     True
3    False
Name: one, dtype: bool

推荐阅读