Home > Net >  Mapping for entering values ​into a dataframe using a dictionary
Mapping for entering values ​into a dataframe using a dictionary

Time:12-23

I have a dictionary that looks like this: {1:"A", 2:"B", 3:"C"}

And dataframe:

col_1 col_2
1,3,2
1
2,3

How can I organize the mapping so that I get the following result:

col_1 col_2
1,3,2 A,C,B
1 A
2,3 B,C

CodePudding user response:

this worked for me

import pandas as pd
alphabet = ['A','B','C','D','E','F','G','H','I']
d = {1:"A", 2:"B", 3:"C"}
s = pd.Series([(1,2,3),1,1,3])
df = pd.DataFrame(s, columns = ["col1"])
col2 = pd.Series([ alphabet[i-1] if isinstance(i,int) else [alphabet[j-1] for j 
in i] for i in s ])
df2 = pd.DataFrame({"col1":s, "col2":col2}

CodePudding user response:

Use nested list comprehension with mapping by dict.get - if not match not replaced:

d = {1:"A", 2:"B", 3:"C"}
df['col_2'] = [','.join(d.get(int(y), y) for y in x.split(',')) for x in df['col_1']]
print (df)
   col_1  col_2
0  1,3,2  A,C,B
1      1      A
2    2,3    B,C

Possible solution if value not exist in dictionary - second solution omit not matched value (7):

print (df)
     col_1
0  1,3,2,7
1        1
2      2,3

d = {1:"A", 2:"B", 3:"C"}
df['col_2_1'] = [','.join(d.get(int(y), y) for y in x.split(',')) for x in df['col_1']]

df['col_2_2'] = [','.join(d.get(int(y)) for y in x.split(',') if int(y) in d) for x in df['col_1']]
print (df)
     col_1  col_2_1 col_2_2
0  1,3,2,7  A,C,B,7   A,C,B
1        1        A       A
2      2,3      B,C     B,C
  • Related