Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map column using two dictionaries

Tags:

python

pandas

I have a df:

ColA  ColB
1     1
2     3
2     2
1     2
1     3
2     1

I would like to use two different dictionaries to change the values in ColB. I would like to use d1 if the value in ColA is 1 and d2 if the value in ColB is 2.

d1 = {1:'a',2:'b',3:'c'}
d2 = {1:'d',2:'e',3:'f'}

Resulting in:

ColA  ColB
1     a
2     f
2     e
1     b
1     c
2     d

How would be the best way of achieving this?

like image 957
OD1995 Avatar asked Apr 29 '19 14:04

OD1995


People also ask

Can you merge two dictionaries?

You can merge two dictionaries by iterating over the key-value pairs of the second dictionary with the first one.

Can map function be used for dictionary?

We can use the Python built-in function map() to apply a function to each item in an iterable (like a list or dictionary) and return a new iterator for retrieving the results.

How do I merge two dictionaries with the same key in Python?

Example 1: Using the | Operator In Python 3.9 and later versions, the | operator can be used to merge dictionaries. Note: If there are two keys with the same name, the merged dictionary contains the value of the latter key.


1 Answers

One way is using np.where to map the values in ColB using one dictionary or the other depending on the values of ColA:

import numpy as np
df['ColB'] = np.where(df.ColA.eq(1), df.ColB.map(d1), df.ColB.map(d2))

Which gives:

    ColA ColB
0     1    a
1     2    f
2     2    e
3     1    b
4     1    c
5     2    d

For a more general solution, you could use np.select, which works for multiple conditions. Let's add another value in ColA and a dictionary, to see how this could be done with three different mappings:

print(df)
    ColA ColB
0     1     1
1     2     3
2     2     2
3     1     2
4     3     3
5     3     1

values_to_map = [1,2,3]
d1 = {1:'a',2:'b',3:'c'}
d2 = {1:'d',2:'e',3:'f'}
d3 = {1:'g',2:'h',3:'i'}

#create a list of boolean Series as conditions
conds = [df.ColA.eq(i) for i in values_to_map]
# List of Series to choose from depending on conds
choices = [df.ColB.map(d) for d in [d1,d2,d3]]
# use np.select to select form the choice list based on conds
df['ColB'] = np.select(conds, choices)

Resulting in:

    ColA ColB
0     1    a
1     2    f
2     2    e
3     1    b
4     3    i
5     3    g
like image 154
yatu Avatar answered Oct 06 '22 15:10

yatu