Han Zhengzu
Han Zhengzu

Reputation: 3852

Mapping function with specific string in Pandas

Here is my attempt.

The Dataframe I have now has a column which will decide how to deal with the features.

For example, df has two columns, DATA and TYPE. The TYPE has three classes: S1,S2 and S3. And I will define three different function based on the different type of samples.

#### S1
 def f_s1(data):
     result = data+1
     return result

#### S2
 def f_s2(data):
     result = data+2
     return result

#### S3
 def f_s3(data):
     result = data+3
     return result

I also created a mapping set:

f_map= {'S1':f_s1,'S2':f_s2, 'S3': f_s3}

Then, I use pandas.Map utility to map these function with the type of each sample.

df['result'] = df['TYPE'].map(f_map)(df['DATA'])

But It didn't work with TypeError: 'Series' object is not callable.

Any advice would be appreciate!

Upvotes: 0

Views: 76

Answers (1)

akuiper
akuiper

Reputation: 215107

df['TYPE'].map(f_map) creates a series of functions, and if you want to apply them to your data column correspondingly, one option would be to use zip() function as follows:

df['result'] = [func(data) for func, data in zip(df['TYPE'].map(f_map), df['DATA'])]
df

enter image description here

Alternatively, you can group by TYPE and then apply the specific function for each type(or group) to the DATA column in that group, assuming your predefined functions contain vectorized operation and thus accepting series as parameters:

df = pd.DataFrame({'TYPE':['S1', 'S2', 'S3', 'S1'], 'DATA':[1, 1, 1, 1]})

df['result'] = (df.groupby('TYPE').apply(lambda g: f_map.get(g['TYPE'].iloc[0])(g['DATA']))
                  .reset_index(level = 0, drop = True))

enter image description here

Upvotes: 1

Related Questions