0

I need to change all my columns value for some items of my list of list:

import pandas as pd
df = pd.read_csv('test.csv')

df 
Sitio,Country,Subastas,Impresiones_exchange
rgg,Perú,32,80   
rgg,Perú,30,49  
rgg,Estados Unidos,24,15

geos = [['Estados Unidos','US - Estados Unidos'],['Estonia','EE - Estonia']]

I'm trying to change all items of df['Country'] to geo[1:] if item of df['Country'] == geo[:1]

for geo in geos:
    for item in df['Country']:
        if item == geo[:1]:
            item = geo[1:]

I need something like this:

Sitio,Country,Subastas,Impresiones_exchange
rgg,Perú,32,80   
rgg,,Perú,30,49  
rgg,US - Estados Unidos,24,15
cs95
  • 379,657
  • 97
  • 704
  • 746
  • 1
    `df.Country.replace(dict(geos))` – cs95 Mar 19 '18 at 19:40
  • @cᴏʟᴅsᴘᴇᴇᴅ Rather, `df.Country.replace(dict(geos), inplace=True)`. – DYZ Mar 19 '18 at 19:42
  • it works but when I print `df`I see the same `df` as before @cᴏʟᴅsᴘᴇᴇᴅ –  Mar 19 '18 at 19:44
  • `df = df.Country.replace(dict(geos))` – cs95 Mar 19 '18 at 19:45
  • It's not working because remember I have more information also the `Country`as `Sitio, Subastadas, Impresiones_exchange`@cᴏʟᴅsᴘᴇᴇᴅ –  Mar 19 '18 at 19:47
  • `df['Country'] = df.Country.replace(dict(geos))` Thanks @cᴏʟᴅsᴘᴇᴇᴅ –  Mar 19 '18 at 19:50
  • I'm trying to save the csv with `encoding='ascii'` or `encoding='utc-8'` but I'm still seeing `PE - Perú`, how can I fix it? @cᴏʟᴅsᴘᴇᴇᴅ –  Mar 19 '18 at 20:18
  • `encoding='latin-1'`? – cs95 Mar 19 '18 at 20:19

0 Answers0