Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
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?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!