0

I have a complicated nested dictionary which I have to use to replace values in specific columns.

dict_map = {
        'ColA' : {('A','Distinct (Highest)','B','C'):'Pass'},
        'ColB' : {'Great': 'A', 'Avg': 'Average', 'F (Absent)': 'Fail'}
    }

cols_to_map = ['ColA','ColB']
new_cols = ['ColA-new','ColB-new']

df = pd.DataFrame(
    {
            'ID': ['AB01', 'AB02', 'AB03', 'AB04', 'AB05','AB06'],
            'ColA': ["A","B","A",np.nan,
                     "C",np.nan],
            'ColB': ['Great', 'Avg', np.nan, np.nan, 'F (Absent)', np.nan]
    })

The output dataframe must contain the 2 new columns, along with the existing data. How can I achieve this in python?

Fazli
  • 171
  • 8
  • 1
    [`DataFrame.replace`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.replace.html) -> `df[new_cols] = df[cols_to_map].replace(dict_map)` – Henry Ecker Oct 15 '21 at 18:06
  • What if the dictionary is like the one above? – Fazli Oct 15 '21 at 18:14
  • 1
    It appears you've changed the question. Your initial dictionary was in valid format for nested replacement. You'd have to reformat the dictionary to be valid key:value pairs for replacement. `{k: v for keys, v in {('A', 'Distinct (Highest)', 'B', 'C'): 'Pass'}.items() for k in keys}` -> `{'A': 'Pass', 'Distinct (Highest)': 'Pass', 'B': 'Pass', 'C': 'Pass'}` – Henry Ecker Oct 15 '21 at 18:17
  • Understood, thank you – Fazli Oct 15 '21 at 18:18
  • I want to keep values which don't match as nan in the new column. For Ex, in ColB if there's a value as X then I want the new column to have nan since X is not mentioned in the dictionary. How can I achieve this? Using the above code, if there's an X then the new column will also have X, I want to avoid that – Fazli Oct 25 '21 at 07:55

0 Answers0