gpt4 book ai didi

python Pandas : Create a new column with values in English by converting values stored in a different column in Chinese traditional

转载 作者:太空宇宙 更新时间:2023-11-04 02:24:27 25 4
gpt4 key购买 nike

我在 pandas 数据框 "df" 中有一列 "City_trad_chinese",其中包含繁体中文的值。我需要创建另一列 "City_English",其中必须包含翻译后的英文值。

我如何使用 Python 执行此操作?我尝试了以下方法:

#importing required libraries
import pandas as pd

from os import path

from googletrans import Translator

#setting path to data
path2data = 'C:/Users/data'

# data import
df = pd.read_excel(path.join(path2data, 'data.xlsx'), converters={'City_trad_chinese':str})


translator = Translator()

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

但它给我一个错误:

raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting value

最佳答案

您可以使用库 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

注意:Google 翻译 API 有一个 15k character limit .您可以通过单独翻译每一行来绕过这一点:

df["City_English"] = ""

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

关于 python Pandas : Create a new column with values in English by converting values stored in a different column in Chinese traditional,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50796361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com