pythonpandastextblob

Return a TextBlob positive, negative or netutral classification in a new column in dataframe


I'm trying to insert a new dataframe column with only the 'positive' or 'negative' string according to TextBlob classification ex: for the 1st line of my df the result is (pos, 0.75, 0.2499999999999997) and I would like to have 'positive' in a new column named 'algo_sentiment', I've been trying with this code:

def sentiment_algo(text):
    try:
        if TextBlob (text, analyzer=NaiveBayesAnalyzer()).sentiment == neg:
          return 'negative'
        return 'positive'
    except:
        return None

df_test['algo_sentiment'] = df_test['cleaned_tweets'].apply(sentiment_algo)

The new colum is indeed created, but returns always either everything positive or everything negative. I've runned some tests and cannot find a solution.


Solution

  • The sentiment property returns a named tuple of (classification, p_pos, p_neg):

    >>> TextBlob('love peace', analyzer=NaiveBayesAnalyzer()).sentiment
    Sentiment(classification='pos', p_pos=0.700187151810585, p_neg=0.2998128481894153)
    

    So change the function to test sentiment.classification:

    def sentiment_algo(text):
        try:
            sentiment = TextBlob(text, analyzer=NaiveBayesAnalyzer()).sentiment
            return 'positive' if sentiment.classification == 'pos' else 'negative'
        except:
            return None
    

    Toy example:

    df_test = pd.DataFrame({'cleaned_tweets': ['love peace', 'worst day ever']})
    df_test['algo_sentiment'] = df_test['cleaned_tweets'].apply(sentiment_algo)
    
    #    cleaned_tweets  algo_sentiment
    # 0      love peace        positive
    # 1  worst day ever        negative