Home > Mobile >  Replace a value found in a Df with a value found in a Dictionary
Replace a value found in a Df with a value found in a Dictionary

Time:11-05

I am having troubles writing an operation that replaces values found in a df with values that are found in a dictionary. Here's what I have:

import pandas as pd

d=[['English','Alabama','bob','smith']]
df=pd.DataFrame(d,columns=["lang",'state','first','last'])
filename='temp'

error_dict={}
error_dict[filename]={}

replace_dict={"state":{"Alabama": "AL", "Alaska": "AK"},"lang":{"English":"ENG", "French":"FR"}}
    
def replace(df, error_dict,colname):
#replace the values found the in df with the value that in the replace_dict
   return df, error_dict

parsing_map={
    "lang": [replace],
    "state":[replace]
    }

for i in parsing_map.keys():
    for j in parsing_map[i]:
        df, error_dict = j(df, error_dict, i)

I am trying to run some operation in the 'replace' function that will replace the values found in df columns 'lang' and 'state' with their abbreviated value from the dictionary 'replace_dict'. It makes it a bit more confusing since, instead of trying to reference the column name, you just use colname.

I have tried to do something like:

def replace(df, error_dict,colname):
    for colname, value in replace_dict.items():
        df[colname] = df[colname].replace(value)
        return df, error_dict

this only replaces the state column value and not for the state and lang. I want it to be in a function i know its not necessary for this exact application but the end result it is nicer.

The desired output is:

d = [['ENG','AL','bob','smith']]
df = pd.DataFrame(d,columns=["lang",'state','first','last'])

How would I create an operation to replace the values in the df with the values in the dictionary using the replace function and 'colname' to reference the name of the column aka "lang" and "state"?

CodePudding user response:

Loop through the dict and replace each column one at a time:

for colname, value in replace_dict.items():
    df[colname] = df[colname].map(value)

EDIT full answer:

import pandas as pd

d=[['English','Alabama','bob','smith']]
df=pd.DataFrame(d,columns=["lang",'state','first','last'])

replace_dict={"state":{"Alabama": "AL", "Alaska": "AK"},"lang":{"English":"ENG", "French":"FR"}}

for colname, value in replace_dict.items():
    df[colname] = df[colname].map(value)
    
  • Related