Sentiment Analysis Multi-language

FREEMIUM
Health Check

N/A

Back to All Tutorials (1)

How to use Sentiment Analysis Multi-Language API with Pandas DataFrames using Apply methods

Apply Sentiment Analysis Multi-Language API to Pandas DataFrame

In Text Processing, it is often necessary to perform operations (such as cleaning, tokenization, spliting, or predicting using ML models) on a certain text row or column to obtain the transformed text data. Writing a for-loop to iterate through Pandas DataFrame and Series will do the job, but that doesn’t seem like a good idea. The for-loop tends to have more lines of code, less code readability, and slower performance.

Fortunately, there are already great methods that are built into Pandas to help you accomplish the goals! In this article, we will see how to perform operations using apply().

Sentiment Analysis Multi-Language API

Here we are using RapidAPI Sentiment Analysis Multi-Language API. This API payload is a json object containing text key and value as a text, the output is also a json object with two fields, prediction and confidence.

Request payload

  • text (string): Text to be predicted

Response payload

  • prediction (string): Model prediction
  • confidence (float): Model score for that prediction

How to use in Pandas

The Pandas apply() is used to apply a function along an axis of the DataFrame or on values of Series.
Let’s begin with a simple example, to sum each row and save the result to a new column “D”

Let’s call the Sentiment Analysis Multi-Language API using apply() function

Let’s see how it’s works with Pandas.


import requests

url = "https://sentiment-analysis-multi-language.p.rapidapi.com/sentiment"

headers = {
			'content-type': "application/json",
			'x-rapidapi-key': "YOUR_API_KEY,
			'x-rapidapi-host': "sentiment-analysis-multi-language.p.rapidapi.com"
}

def sentiment_analysis(row):
		payload = {
				"text": row
		}
		
		response = requests.request("POST", url, json=payload, headers=headers)

		return response.json()["prediction"]
		
		
df['sentiment'] = df["text"].apply(sentiment_analysis)