简体   繁体   中英

sentiment analysis of a dataframe using if else statements

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.

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM