我如何将这个GoogleAPI翻译追加回我的数据框架?

2024-10-04 11:32:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试翻译(从自动检测到英语)这个熊猫数据帧。我不能只是将数据帧插入转换器。我认为它需要转换成JSON,然后翻译成字典,然后移回数据帧。我希望最终将其作为一个新列合并回原始数据帧。我的输出不是我期望的。希望你能帮忙

import pandas as pd
from google.cloud import translate_v2 as translate
import os
import json

d = {'compression': ['Ttnft, تارودانت, 111111', 'Sstma, Санкт-Петербург, B-8601 Gzprmbnk, Екатеринбург HUJ852']}
df = pd.DataFrame(data=d)

inputStrings = df['compression'].to_json()
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "Google secure account ID"

translate_client = translate.Client()

result = translate_client.translate(inputStrings, target_language='en')

df2 = pd.DataFrame(data = result, index=[0])
print(df2)

输出:

                                      translatedText detectedSourceLanguage                                              input
0  {"0":"Rsst, 11211, Brooklyn","1":"Ttnft, \u062...                     en  {"0":"Rsst, 11211, Brooklyn","1":"Ttnft, \u062...

Tags: 数据importclientjsondataframedfdataos