Dan
Dan

Reputation: 1587

Using .apply in python to apply a mapper

this should be very simple but I can't figure it out.

I have a 'mapper' DataFrame that looks something like this:

mapper={'old_values':[105,312,269],'new_values':[849,383,628]}
df=pd.DataFrame(mapper)

I then have another dataframe with a column that contains old values. I simply want to convert them all to their new values (e.g. all 105's should become 849's). I think I need to use df.apply but I can't find an example of how to do this.

Thanks in advance.

Upvotes: 1

Views: 51

Answers (1)

Nickil Maveli
Nickil Maveli

Reputation: 29721

It's better to choose Series.map method which performs similar to a python dictionary in functionality to aid in the mapping of values from one series to another than go for a slow apply function here.

df['old_values'].map(df.set_index('old_values')['new_values'])
Out[12]:
0    849
1    383
2    628
Name: old_values, dtype: int64

The only modification you need to make here is:

new_df['old_values'].map(old_df.set_index('old_values')['new_values'])

But do note that this introduces NaN for keys not found in the original DF.(Any unseen value encountered by the new DF would be coerced to NaN).

If this is the behavior you'd expect then map is an ideal method.


Although, if your intention is to simply replace the values and leave the missing keys as they were before, you can opt for Series.replace method.

new_df['old_values'].replace(old_df.set_index('old_values')['new_values'])

Upvotes: 1

Related Questions