Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to do a Series.map in place, but keep original value if no match?

The scenario here is that I've got a dataframe df with raw integer data, and a dict map_array which maps those ints to string values.

I need to replace the values in the dataframe with the corresponding values from the map, but keep the original value if the it doesn't map to anything.

So far, the only way I've been able to figure out how to do what I want is by using a temporary column. However, with the size of data that I'm working with, this could sometimes get a little bit hairy. And so, I was wondering if there was some trick to do this in pandas without needing the temp column...

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(1,5, size=(100,1)))
map_array = {1:'one', 2:'two', 4:'four'}

df['__temp__'] = df[0].map(map_array, na_action=None) 
#I've tried varying the na_action arg to no effect

nan_index = data['__temp__'][df['__temp__'].isnull() == True].index
df['__temp__'].ix[nan_index] = df[0].ix[nan_index]
df[0] = df['__temp__']
df = df.drop(['__temp__'], axis=1)
like image 427
badideas Avatar asked Nov 12 '13 02:11

badideas


1 Answers

I think you can simply use .replace, whether on a DataFrame or a Series:

>>> df = pd.DataFrame(np.random.randint(1,5, size=(3,3)))
>>> df
   0  1  2
0  3  4  3
1  2  1  2
2  4  2  3
>>> map_array = {1:'one', 2:'two', 4:'four'}
>>> df.replace(map_array)
      0     1    2
0     3  four    3
1   two   one  two
2  four   two    3
>>> df.replace(map_array, inplace=True)
>>> df
      0     1    2
0     3  four    3
1   two   one  two
2  four   two    3

I'm not sure what the memory hit of changing column dtypes will be, though.

like image 165
DSM Avatar answered Oct 14 '22 22:10

DSM