1

I want to remap values in several columns of my DataFrame: from [site1] to [site5].

Here is my dictionary:

new_dict
{'accounts.google.com': 5,
'apis.google.com': 7,
'football.kulichki.ru': 9,
'geo.mozilla.org': 3,
'google.com': 4,
'mail.google.com': 6,
'meduza.io': 10,
'oracle.com': 2,
'plus.google.com': 8,
'vk.com': 1,
'yandex.ru': 11}

    site1   site2       site3       site4           site5        user       
0   vk.com  oracle.com  oracle.com  geo.mozilla.org oracle.com   1      

1   vk.com  google.com  google.com  google.com       0           2

Is there a way to remap these columns at the same time? I'm trying this approach, but it doesn't work in any way...

df_train['site%d' %(range(1,11))].replace(new_dict)     
elenaby
  • 167
  • 2
  • 11

1 Answers1

4

You can filter columns by mask with startswith and loc and apply replace:

m = df_train.columns.str.startswith('site')
df_train.loc[:, m] = df_train.loc[:, m].replace(new_dict)

Or:

df_train.update(df_train.loc[:, m].replace(new_dict))

print (df_train)
   site1  site2  site3  site4 site5  user
0      1      2      2      3     2     1
1      1      4      4      4     0     2

Detail:

print (m)
[ True  True  True  True  True False]


print (df_train.loc[:, m])
    site1       site2       site3            site4       site5
0  vk.com  oracle.com  oracle.com  geo.mozilla.org  oracle.com
1  vk.com  google.com  google.com       google.com           0

If never values from dict in another columns simpliest is:

df_train = df_train.replace(new_dict)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252