The world’s leading publication for data science, AI, and ML professionals.

Google Translate API for Python

In this tutorial. I will demonstrate how to use the Google Translate API for translating the data from Hindi to English.

Image Credits: API Evangelist
Image Credits: API Evangelist

There are some things I want to clear at the beginning of the tutorial. First, I would translate the data from Hindi to English. Also, I will demonstrate the translation on a Pandas Data Frame, meaning I will convert the entire data frame from Hindi to English. Isn’t that great? Well, let’s get started. Also, you can use the Google Colab notebook to type the code. I would recommend everyone go through the documentation of the Google Translate API for Python completely, so by the time you start to code you would probably know what I mean. It would be a cakewalk because you would know most of the terms while coding.

googletrans

Open your Google Colab and create a new notebook and name it as "_Google _Translations.ipynb_". Before actually typing the code I want all of you guys manually install the Google Translate library inside the notebook. So to do this just type !pip install googletrans . This command automatically downloads and installs the library as shown below:

# install googletrans using pip
!pip install googletrans
!pip install googletrans
!pip install googletrans

Importing the necessary libraries

In this step, we will be importing the necessary libraries that we will be using throughout the tutorial. The library pandas are for storing the CSV data into a data frame. And googletrans is obviously used for translation and we will also use one of its methods called Translator which you will see in the later tutorial.

# Importing the necessary libraries
import pandas as pd
import googletrans
from googletrans import Translator

Storing the CSV file as a data frame

In this step, we will be storing the CSV file as a data frame with the help of pandas. For you to get the CSV file, click the link below:

Vegetables_names_in_hindi.csv

After downloading the CSV file, upload the file to Google Colab. There are 3 horizontal lines on the left-hand side, on hovering it prompts "Show table of contents". After clicking go the "Files" tab and then press "Upload". Then upload the CSV.

Uploading the CSV file
Uploading the CSV file

Now you have to read the CSV file and store it in a data frame. For clarity, I am displaying the first 10- rows of the data frame.

# Reading and storing the CSV file as a dataframe
df = pd.read_csv('/content/Vegetables_names_in_hindi.csv')
df.head(10)
CSV file stored as a data frame with the help of pandas library
CSV file stored as a data frame with the help of pandas library

Building the translator function and translating the data frame

This is w[here](http://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 translations) we actually translate the data frame from Hindi to English. The original code was retrieved from an [article](http://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 translations). But I made some changes here and there. So to know more about translation function click here.

Translate a Pandas data frame using googletrans library

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
translations
Hindi to English values stored in a dictionary
Hindi to English values stored in a dictionary

Basically, I am storing all the unique elements in a data frame and then translating every element into English with the help of the translator function (Translator()). By doing so, as you can see from the above output, all the elements are now being translated from Hindi to English which is stored in a dictionary.


Replacing the translated words to the original data frame

Now the final step is to replace or save the translated data into a new or the original data frame. Here I would be replacing it with the original data frame with the help of the pandas replace function.

# Replacing all the translated words from the dictionary to the original dataframe
df.replace(translations, inplace = True)
df.head(10)
Finally translated data (English)
Finally translated data (English)

You have successfully translated the data from the CSV file from the Hindi language to English. In the future, you can use this tutorial as a reference to translate the data from a different language to English by default. I hope you guys learned something new today. If you have any doubts about the tutorial, don’t hesitate to ask in the comment section below. Until then, see you next time. Goodbye.


Related Articles