0

I have a dataframe with below values

Text                                State
This is a beutiful day              California
But I am stuck with code            New York
It's too hard                       California
Can somebody please help me         Florida
I am new to python                  Florida
How should I solve this problem     New York
Day is turning bad                  New York
I am getting exhaused               California
Need some help                      New York

I need to calculate sentiment analysis on the text state wise. How may I aggregate the text state wise and then do sentiment analysis.

df = df.groupby(df.columns.difference(['Text']))
result = pd.DataFrame(df['State'].unique(), columns=df.columns)

I am trying to do it this way to aggregate text first but its not working. Need some suggestions for aggregation and then how to perform sentiment analysis using loop in the dataframe.

1 Answer 1

1

You can group the text using lambda function which takes the text and joins using the delimiter provided.

`delimiter = ' '
df2 =  df.groupby('State')['Text'].apply(lambda x: "%s" % delimiter.join(x)).reset_index()
print (df2)`

Adding reset.index() would convert into your required dataframe

Sign up to request clarification or add additional context in comments.

3 Comments

apply(delimiter.join) is enough.
Thanks, it worked. Could you also suggeest how should I do sentiment analysis on each row of the 'df2' dataframe now.
and how to preprocess and lemmitize the text column to remove all the stopwords, punctuation and digits

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.