Home:ALL Converter>Replacing DataFrame values with dictionary matches and a string for non-matches

Replacing DataFrame values with dictionary matches and a string for non-matches

Ask Time:2020-05-12T05:16:56         Author:Michael Kessler

Json Formatter

I have DataFrame:

df = pd.DataFrame{'col1': ['afs', 'chk', 'est', 'app'],
 'col2': ['ofcr', 'guar', 'ltv', 'gender'],
 'col3': ['code', 'mod']}

And I have dictionary:

dict = {'ofcr':'officer','chk':'check','mod':'modification','est':'estimated','app':'application', 'gender':'gender'}

I need to iterate over df and replace mathing keys with their respective values. I can do this column by column with:

df["col1"] = df["col1"].map(dict)

But this converts non-matches to NaN. What I want is to leave the token unchanged, but add "-UNKNOWN-" or something similarly obvious to the string so it can be dealt with later. I've tried loops:

for tok in df['col1']:
    if tok in dict.values():
        df.replace(dict, inplace=True)
    if tok not in dict.values():
        df.replace(tok, tok '-UNKNOWN', inplace=True)
    print(tok)

This also replaced the matches (oddly enough in all columns, not just the one passed in) but didn't affect the non-matches.

Author:Michael Kessler,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/61739492/replacing-dataframe-values-with-dictionary-matches-and-a-string-for-non-matches
Andrej Kesely :

You can use applymap():\n\ndf = pd.DataFrame({'col1': ['afs', 'chk', 'est', 'app'],\n 'col2': ['ofcr', 'guar', 'ltv', 'gender'],\n 'col3': ['code', 'mod', 'xxx', 'zzz']})\n\ndct = {'ofcr':'officer','chk':'check','mod':'modification','est':'estimated','app':'application', 'gender':'gender'}\n\nprint(df.applymap(lambda x: dct.get(x, x + '-UNKNOWN')))\n\n\nPrints:\n\n col1 col2 col3\n0 afs-UNKNOWN officer code-UNKNOWN\n1 check guar-UNKNOWN modification\n2 estimated ltv-UNKNOWN xxx-UNKNOWN\n3 application gender zzz-UNKNOWN\n",
2020-05-11T21:30:59
yy