1

I'm trying to iterate through a DataFrame with a for loop but I'm getting this error:

"ValueError: The truth value of a Series is ambiguous."

My dataframe is: Dataframe

And I would like to iterate through "Plataforma " and "Soporte" to replace 'Soporte' values. I am using this:

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')

Thanks in advance.

A. Esquivias
  • 230
  • 3
  • 10

1 Answers1

2

First iterrows is one of slowiest iterating solution in pandas, better is avoid it, check this answer by pandas developer Jeff.

So you can create dictionaries for replacement, filter rows by mask with DataFrame.loc and use 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)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252