我试图使用for循环遍历DataFrame,但是我得到了这个错误:
"ValueError:序列的真值不明确。“
我的数据框是:

我想遍历"Plataforma“和"Soporte”来替换“Soporte”值。我使用的是:
for index, row in informe.iterrows():
if informe.loc[index, 'Plataforma'] == 'Taboola':
informe['Soporte'].str.replace('performance-prospecting_tconvergentes', 'Prospecting_Taboola_tconvergentes')
informe['Soporte'].str.replace('performance-prospecting_tmoviles', 'Prospecting_Taboola_tmoviles')
informe['Soporte'].str.replace('performance-prospecting', 'Prospecting_Taboola')
elif informe.loc[index, 'Plataforma'] == 'Yahoo':
informe['Soporte'].str.replace('performance-prospecting_tconvergentes', 'Prospecting_Yahoo_tconvergentes')
informe['Soporte'].str.replace('performance-prospecting_tmoviles', 'Prospecting_Yahoo_tmoviles')
informe['Soporte'].str.replace('performance-prospecting', 'Prospecting_Yahoo')提前谢谢。
发布于 2020-01-27 22:58:37
First iterrows是pandas中最灵活的迭代解决方案之一,最好避免它,请查看this answer by pandas developer Jeff。
因此,您可以使用DataFrame.loc和Series.replace创建替换字典、按掩码筛选行
d1= {'performance-prospecting_tconvergentes': 'Prospecting_Taboola_tconvergentes',
'performance-prospecting_tmoviles': 'Prospecting_Taboola_tmoviles',
'performance-prospecting': 'Prospecting_Taboola'}
d2 = {'performance-prospecting_tconvergentes': 'Prospecting_Yahoo_tconvergentes',
'performance-prospecting_tmoviles': 'Prospecting_Yahoo_tmoviles',
'performance-prospecting':'Prospecting_Yahoo'}
m1 = informe['Plataforma'] == 'Taboola'
m2 = informe['Plataforma'] == 'Yahoo'
informe.loc[m1, 'Soporte'] = informe.loc[m1, 'Soporte'].replace(d1)
informe.loc[m2, 'Soporte'] = informe.loc[m2, 'Soporte'].replace(d2)https://stackoverflow.com/questions/59933526
复制相似问题