Skip to content Skip to sidebar Skip to footer

Python Pandas: Create A New Column With Values In English By Converting Values Stored In A Different Column In Chinese Traditional

I have a column 'City_trad_chinese' in a pandas dataframe 'df' which contains values in Traditional Chinese language. I need to create another column 'City_English' which must cont

Solution 1:

You can use the library googletrans

import pandas as pd
from googletrans import Translator

d = {"City_trad_chinese":["香港特别行政区",
                          "澳门特别行政区",
                          "北京市",
                          "上海市"]}
df = pd.DataFrame(data=d)

translator = Translator()

df["City_English"] = df["City_trad_chinese"].map(lambda x: translator.translate(x, src="zh-TW", dest="en").text)

print(df["City_English"])

0    Hong Kong Special Administrative Region
1        Macao Special Administrative Region
2                               Beijing City
3                              Shanghai City

Note: The Google Translate API has a 15k character limit. You can circumnavigate this by translating each row individually:

df["City_English"] = ""

for index, rowin df.iterrows():
    translator = Translator()
    eng_text = translator.translate(row["City_trad_chinese"], src="zh-TW", dest="en").text
    row["City_English"] = eng_text

Post a Comment for "Python Pandas: Create A New Column With Values In English By Converting Values Stored In A Different Column In Chinese Traditional"