Home > Back-end >  How to map column of lists with values in a dictionary using pandas
How to map column of lists with values in a dictionary using pandas

Time:08-03

I'm new to pandas and I want to know if there is a way to map a column of lists in a dataframe to values stored in a dictionary.

Lets say I have the dataframe 'df' and the dictionary 'dict'. I want to create a new column named 'Description' in the dataframe where I can see the description of the Codes shown. The values of the items in the column should be stored in a list as well.

import pandas as pd

data = {'Codes':[['E0'],['E0','E1'],['E3']]}
df = pd.DataFrame(data)

dic = {'E0':'Error Code', 'E1':'Door Open', 'E2':'Door Closed'}

How the final df should be shown

CodePudding user response:

Most efficient would be to use a list comprehension.

df['Description'] = [[dic.get(x, None) for x in l] for l in df['Codes']]

output:

      Codes              Description
0      [E0]             [Error Code]
1  [E0, E1]  [Error Code, Door Open]
2      [E3]                   [None]

If needed you can post-process to replace the empty lists with NaN, use an alternative list comprehension to avoid non-matches: [[dic[x] for x in l if x in dic] for l in df['Codes']], but this would probably be ambiguous if you have one no-match among several matches (which one is which?).

CodePudding user response:

data = {'Codes':[['E0'],['E0','E1'],['E3']]}

dct = {'E0':'Error Code', 'E1':'Door Open', 'E2':'Door Closed'}

data["Descriptions"] = [
    [
        dct.get(code, np.nan)
        for code in codes
    ]
    for codes in data["Codes"]
]

df = pd.DataFrame(data=data)
  • Related