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

Categories

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

python - How can I append this Google API translation back into my dataframe?

I'm trying to translate (from auto detect to english) this 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. I'd like eventually merge it back into the original dataframe as a new column. My output is not what I expected. Hope you can help!

In

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)

Output:

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

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

1 Answer

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

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