Created
February 27, 2020 01:03
-
-
Save e96031413/05c4ee2bac2aba643d872326f5eb7f73 to your computer and use it in GitHub Desktop.
Translate Your CSV file to specific language with googletrans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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