YanRemes
YanRemes

Reputation: 337

Loop through each value in a dataframe column

I have a dataframe

0         i only need uxy to hit 20 eod to make up for a...
1                                        oh this isn’t good
2         lads why is my account covered in more red ink...
3         i'm tempted to drop my last 800 into some stup...
4         the sell offs will continue until moral improves.

I have a code

my_text = 'i only need uxy to hit 20 eod to make up for a...
                                        oh this isn’t good'

seq = tokenizer.texts_to_sequences([my_text])
seq = pad_sequences(seq, maxlen=maxlen)
prediction = model.predict(seq)

print('positivity:',prediction)

What i want to do is to calculate the positivity for each sentence in each row. It works fine with one my_text but I don't know how to change it in a way to calculate for each sentence. And I would like to create an extra column that would show the positivity for each row sentence Appreciate your help

Upvotes: 0

Views: 95

Answers (1)

ThePyGuy
ThePyGuy

Reputation: 18466

Just create a function with the same exact code that you have with a return statement to return the value, then .apply it on the column you want to calculate the values:

def getPositivity(my_text):
    seq = tokenizer.texts_to_sequences([my_text])
    seq = pad_sequences(seq, maxlen=maxlen)
    prediction = model.predict(seq)
    return prediction

df['prediction'] = df['col'].apply(getPositivity)

Code above assumes that the dataframe variable name is df, and the column name for these string values is col

Upvotes: 1

Related Questions