首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ValueError:序列的真值是ambiguous.in循环

ValueError:序列的真值是ambiguous.in循环
EN

Stack Overflow用户
提问于 2020-01-27 22:53:52
回答 1查看 57关注 0票数 1

我试图使用for循环遍历DataFrame,但是我得到了这个错误:

"ValueError:序列的真值不明确。“

我的数据框是:

我想遍历"Plataforma“和"Soporte”来替换“Soporte”值。我使用的是:

代码语言:javascript
复制
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')

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-27 22:58:37

First iterrows是pandas中最灵活的迭代解决方案之一,最好避免它,请查看this answer by pandas developer Jeff

因此,您可以使用DataFrame.locSeries.replace创建替换字典、按掩码筛选行

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59933526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档