Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map multiple columns simultaneously with different maps

I have a pandas data frame here:

import pandas as pd

df = pd.DataFrame({'col1': ['a','a','b','b'],
                   'col2': ['a','a','b','b']})

I have a mapping dictionary here:

dict_map = {'col1': {'a': 0.5, 'b': 1.0},
            'col2': {'a': 0.6, 'b': 0.9}}

I want to map each column with the corresponding dictionary so that the data frame will look like:

    col1  col2
0   0.5   0.6
1   0.5   0.6
2   1.0   0.9
3   1.0   0.9

I can easily iterate through the columns in df and achieve the desired result, but I am trying to avoid iterating. How can I do this without iterating?

like image 786
Aaron England Avatar asked Jul 26 '26 07:07

Aaron England


2 Answers

You can use pandas.DataFrame.replace:

df = df.replace(dict_map)

Output:

>>> df
   col1  col2
0   0.5   0.6
1   0.5   0.6
2   1.0   0.9
3   1.0   0.9

I would use the df.replace(dict) function to avoid loops.

like image 33
Yashar Ahmadov Avatar answered Jul 27 '26 22:07

Yashar Ahmadov