I have a dataframe 'df' which has data like :
State Text
0 California This is a beutiful day# It's too hard I am get...
1 Florida Can somebody please help me; I am new to python
2 New York But I am stuck with code How should I solve th...
This dataframe is created from a csv file using below code :
delimiter = ' '
df = df2.groupby('State')['Text'].apply(lambda x: "%s" % delimiter.join(x)).reset_index()
I need to do sentiment analysis(using TextBlob) on this dataframe 'df' state wise. Could anyone please help me to do the sentiment analysis state wise. I tried to do it as:
for row in df.itertuples():
text = df.iloc[:, 1].tolist()
tweets = " ".join(str(x) for x in text)
text = TextBlob(tweets)
score = text.sentiment
But it gave me sentiment score of total dataframe, not sentiment score for each state separately
My code gave output as :
Sentiment(polarity=-0.07765151515151517, subjectivity=0.49810606060606055)
But i want sentiment output for each row(that means for each state) separately.
You can use apply()
in combination with a lambda
function. This is a much more efficient way than looping.
df[['polarity', 'subjectivity']] = df['Text'].apply(lambda Text: pd.Series(TextBlob(Text).sentiment))
This returns:
State Text polarity subjectivity
0 California This is a beutiful day# It's too hard I am get -0.291667 0.541667
1 Florida Can somebody please help me; I am new to python 0.136364 0.454545
2 New York But I am stuck with code How should I solve th 0.000000 0.000000
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.