using a dictionary function to call a df column

Question:

I have a dictionary that has a list of all of the df column names and maps them to function names.

import pandas as pd
 
data= [["john","","","English","","","","","","","","",""]]
df= pd.DataFrame(data,columns=['firstName', 'lastName', 'state', 'Communication_Language__c', 'country', 'company', 'email', 'industry', 'System_Type__c', 'AccountType', 'customerSegment', 'Existing_Customer__c', 'GDPR_Email_Permission__c'])
filename= 'Template'
 
lang_trans= {"English":"ENG", "French":"FR"}

def lang (lang_trans,df):
    df.replace(lang_trans, inplace=True)
    df.str.upper()
    return df

parsing_map{
"Communication_Language__c": lang}

I would like to replace the data in the df with the abbreviation for ‘english’ as ‘ENG’,
I have tried to achieve this with

def lang (lang_trans,df):
    df.replace(lang_trans, inplace=True)
    df.str.upper()
    return df

but it is not transforming ‘english’ to ‘ENG’ in the df

How would I call the function lang from the dictionary to change the value in the df[Communication_language_c to be ‘ENG’

desired output

data= [["john","","","ENG","","","","","","","","",""]]
df= pd.DataFrame(data,columns=['firstName', 'lastName', 'state', 'Communication_Language__c
Asked By: stack user

||

Answers:

Try something like this:

import pandas as pd
 
data= [["john","","","English","","","","","","","","",""]]
df= pd.DataFrame(data,columns=['firstName', 'lastName', 'state', 'Communication_Language__c', 'country', 'company', 'email', 'industry', 'System_Type__c', 'AccountType', 'customerSegment', 'Existing_Customer__c', 'GDPR_Email_Permission__c'])

filename= 'Template'

 
lang_trans= {"English":"ENG", "French":"FR"}

def update_lang(df_temp, column, lang_dict):
    for k, v in lang_dict.items():
        df_temp[column] = df[column].replace(k,v)
    
    return df_temp

df = update_lang(df, 'Communication_Language__c', lang_trans)
print(df)
Answered By: ScottC

Creating a function is not necessary. I also don’t understand why you are using the line "lang.str.upper()" if you just want to transform "English" to "ENG" and "French" to "FR" so I took it out. Does this get the output you want?

import pandas as pd

data = [["john", "", "", "English", "", "", "", "", "", "", "", "", ""]]
df = pd.DataFrame(data,
                  columns=['firstName', 'lastName', 'state', 'Communication_Language__c', 'country', 'company', 'email',
                           'industry', 'System_Type__c', 'AccountType', 'customerSegment', 'Existing_Customer__c',
                           'GDPR_Email_Permission__c'])
filename = 'Template'
lang_trans = {"English": "ENG", "French": "FR"}
df['Communication_Language__c'].replace(lang_trans, inplace=True)
Answered By: Hanna