I have a dataframe which has a lot of characters that need changing.
I can do this line by line but I couldn't figure out how to loop through these characters to replace with a new character.
This is my code so far:
df_media_input['MediaChannel']=df_media_input['MediaChannel'].map(lambda x: str.replace(x, "Direct Mail","DM"))
df_media_input['MediaChannel']=df_media_input['MediaChannel'].map(lambda x: str.replace(x, "DR TV","DRTV"))
df_media_input['MediaChannel']=df_media_input['MediaChannel'].map(lambda x: str.replace(x, "Affilliates","Affiliates"))
df_media_input['MediaChannel']=df_media_input['MediaChannel'].map(lambda x: str.replace(x, "DRTV","TV"))
df_media_input['MediaChannel']=df_media_input['MediaChannel'].map(lambda x: str.replace(x, "All Time TV","TV"))
df_media_input['MediaChannel']=df_media_input['MediaChannel'].map(lambda x: str.replace(x, "Peak TV","TV"))
df_media_input['MediaChannel']=df_media_input['MediaChannel'].map(lambda x: str.replace(x, "Regional Press","Press"))
df_media_input['MediaChannel']=df_media_input['MediaChannel'].map(lambda x: str.replace(x, "National Press","Press"))
but I feel something like this should be possible:
dic= {Direct Mail:DM}
for i and j in dic:
df_media_input['MediaChannel']=df_media_input['MediaChannel'].map(lambda x: str.replace(x, i,j))
where Direct Mail is i and DM is j