Issue
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
Solution
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.