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().
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
Response payload
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 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)