jeudi 10 septembre 2020

Assigning values in pandas dataframe using nested if statements

I would like to assign values based on some logical condition. For example:

- if a message contains the word hate, the assign -1;
- If a message contains the word HATE, then assign -1.5;
- if a message contains the word Good then assign +1;
- if a message contains the word GOOD, then assign +1.5 ;
- and so on.

So I would have multiple conditions which would allow me to assign a score.

I am trying to implement the above conditions using nested if statements. For example:

  • if I assigned a value of 1.5 because the message contained the word GOOD, I would like to add:
    • 0.5 if there is a ! in the sentences;
    • 0.7 if there is !!;
    • 0.9 if there is !!!

To give you an example:

Message 

This is GOOD! 
I do not like it 
How are you doing? 
You are a very good player...

From the example above, I would like to assign, based on the above conditions:

  • 1.5 to the first sentence, then add 0.7 as it contains one !;
  • 0 to the second sentence;
  • 0 to the third one;
  • 1 to the fourth one.

What I have tried is to set the logic behind using nested if condition, as I mentioned:

if df.Message.str.contains(‘good’):
    df.value=1
    if df.Message.str.contains(‘!’):
    df.value=0.5
    elif df.Message.str.contains(‘!!’):
    df.value=0.7
    elif df.Message.str.contains(‘!!!’):
    df.value=0.9
elif df.Message.str.contains(‘GOOD’):
  df.value=1.5
  if df.Message.str.contains(‘!’):
    df.value=0.5
    elif df.Message.str.contains(‘!!’):
    df.value=0.7
    elif df.Message.str.contains(‘!!!’):
    df.value=0.9

and so on with the other similar cases, i.e. hate and HATE. Unfortunately, I spotted many errors that I have been trying to fix:

- the use of str.contains for specific terms within the column;
- the use of `if/elif`;
- the wrong update of the values on value column.

I hope you can give me some good advice on how to fix it in order to get the expected output.

Aucun commentaire:

Enregistrer un commentaire