Getting output of last line of code only?

51 views Asked by At

Creating column 'Location English' in df. Have to convert location in kannada in 'Location' column to english. The column 'Location English' just has Ajekar in english and all other location in kannada.

df['Location English'] = df['Location'].apply(lambda s: s.replace('ಶಂಕರನಾರಯಣ', 'Shankaranarayana'))
df['Location English'] = df['Location'].apply(lambda s: s.replace('ಕಾರ್ಕಳ', 'Karkala'))
df['Location English'] = df['Location'].apply(lambda s: s.replace('ಅಜೆಕಾರು', 'Ajekar'))
3

There are 3 answers

0
Tristan Reid On BEST ANSWER

Each time you read from df['Location'], which is not changed, so it overwrites the previous values of df['Location English']. You can apply all of your changes like this:

df['Location English'] = df['Location'].apply(lambda s: s.replace('ಶಂಕರನಾರಯಣ', 'Shankaranarayana'))
df['Location English'] = df['Location English'].apply(lambda s: s.replace('ಕಾರ್ಕಳ', 'Karkala'))
df['Location English'] = df['Location English'].apply(lambda s: s.replace('ಅಜೆಕಾರು', 'Ajekar'))
0
ramanpawar On

Every line is an assignment to df['Location English']. That's why you are getting only the last line in df['Location English'].

0
Sebastien D On

Actually, you can chain your replace in one single line :

df['Location English'] = df['Location'].replace('ಶಂಕರನಾರಯಣ', 'Shankaranarayana').replace('ಕಾರ್ಕಳ', 'Karkala').replace('ಅಜೆಕಾರು', 'Ajekar')