Alex
Alex

Reputation: 81

Python: How to aggregate values in DataFrame

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.

Upvotes: 0

Views: 216

Answers (1)

sidm
sidm

Reputation: 96

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

Upvotes: 1

Related Questions