Home > Back-end >  sentiment analysis of a dataframe using if else statements
sentiment analysis of a dataframe using if else statements

Time:03-18

I obtained the adjectives using this function:

def getAdjectives(text):

    blob=TextBlob(text)
    return [ word for (word,tag) in blob.tags if tag == "JJ"]

dataset['adjectives'] = dataset['text'].apply(getAdjectives)`

I obtained the dataframe from a json file using this code:

with open('reviews.json') as project_file:    
    data = json.load(project_file)
dataset=pd.json_normalize(data) 
print(dataset.head())

i have done the sentiment analysis for the dataframe using this code:

dataset[['polarity', 'subjectivity']] = dataset['text'].apply(lambda text: pd.Series(TextBlob(text).sentiment))
print(dataset[['adjectives', 'polarity']])

this is the output:


                                          adjectives  polarity
0                                                 []  0.333333
1  [right, mad, full, full, iPad, iPad, bad, diff...  0.209881
2                             [stop, great, awesome]  0.633333
3                                          [awesome]  0.437143
4                        [max, high, high, Gorgeous]  0.398333
5                                     [decent, easy]  0.466667
6  [it’s, bright, wonderful, amazing, full, few...  0.265146
7                                       [same, same]  0.000000
8         [old, little, Easy, daily, that’s, late]  0.161979
9                       [few, huge, storage.If, few]  0.084762

I have tried to filter the adjectives so as to determine those with positive, neutral and negative polarity in this code:

if dataset['polarity']> 0:
    print(dataset[['adjectives', 'polarity']], "Positive")
        
elif dataset['polarity'] == 0:
    print(dataset[['adjectives', 'polarity']], "Neutral")   
else: 
        print(dataset[['adjectives', 'polarity']], "Negative")

I got the error:

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Kindly help.

CodePudding user response:

Try using np.select to determine the sentiment based on the polarity:

df['sentiment'] = np.select(
    [
        dataset['polarity'] > 0,
        dataset['polarity'] == 0
    ],
    [
        "Positive",
        "Neutral"
    ],
    default="Negative"
)

One-liner:

df['sentiment'] = np.select([dataset['polarity'] > 0, dataset['polarity'] == 0], ["Positive", "Neutral"], "Negative")

CodePudding user response:

If you just want to print the repective parts of dataset:

print('Positive:')
print(dataset.loc[dataset['polarity'] > 0, ['adjectives', 'polarity']])
print('Neutral:')
print(dataset.loc[dataset['polarity'] == 0, ['adjectives', 'polarity']])
print('Negative:')
print(dataset.loc[dataset['polarity'] < 0, ['adjectives', 'polarity']])

See Boolean indexing for details.

  • Related