Skip to content

Instantly share code, notes, and snippets.

@e96031413
Created February 27, 2020 01:03
Show Gist options
  • Save e96031413/05c4ee2bac2aba643d872326f5eb7f73 to your computer and use it in GitHub Desktop.
Save e96031413/05c4ee2bac2aba643d872326f5eb7f73 to your computer and use it in GitHub Desktop.
Translate Your CSV file to specific language with googletrans
# install googletrans using pip
!pip install googletrans
# Importing the necessary libraries
import pandas as pd
import googletrans
from googletrans import Translator
# Reading and storing the CSV file as a dataframe
df = pd.read_csv('path/to/yourFile.csv')
df.head(10)
translator = Translator()
translations = {}
for column in df.columns:
# Unique elements of the column
unique_elements = df[column].unique()
for element in unique_elements:
# Adding all the translations to a dictionary (translations)
translations[element] = translator.translate(element).text
# Replacing all the translated words from the dictionary to the original dataframe
df.replace(translations, inplace = True)
df.head(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment