Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
680 views
in Technique[技术] by (71.8m points)

python - How can I translate this dataframe through Google Translator API?

I'm trying to translate (from auto detect to english) each of these rows in a pandas Dataframe. I can't just plug in the dataframe into translator. I think it needs to be converted to JSON and then translated into a dictionary, and then moved back into a dataframe.

The dataframe comes out looking weird and I'm really confused

Is there a better way to do this?

Here's my code:

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)

Ultimately I'd like to append the new translation back to the original dataframe. I'm not sure whether it's better to replace the existing column or add a new one. Hope you can help! thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...