0

So I am running live data and want to be notified if the column Line Variance is greater than five. That also needs to depend on other column values as well, such as the half being "1st half", Time Remaining being over 10, and the side being "Favorite". I'm having difficulty writing my if statement so that it searches for all these in the same row. I am using Twilio to notify me, and I am trying to include the event name, live line, line variance, team, and time remaining in the message of the row that is true. Does anyone know how I can do this?

if np.any(df['Line Variance'] <= 5) and np.any(df['Side'] == "Favorite"):
    # send message
        client.messages.create(to="+X", 
                       from_="+X", 
                       body= np.any(df['Line Variance']))
        print('message sent')
    

Example data

Data Picture

Reti43
  • 9,656
  • 3
  • 28
  • 44
alty1723
  • 11
  • 2

1 Answers1

0

When you have a conditional about a column, the result will be a boolean for all rows. Reconstructing the table in your example, df['Line Variance'] <= 5 would return

0     True
1    False
2     True
3     True
Name: Line Variance, dtype: bool

Then a boolean AND operation will return true if and only if both inputs are true. This way you can combine many conditionals. For example, (df['Line Variance'] <= 5) & (df['Side'] == "Favorite") would return

0    False
1    False
2     True
3    False
dtype: bool

And then you can use this to filter for specific rows.

interested = (df['Line Variance'] <= 5) & (df['Side'] == "Favorite")
print(df[interested])

Which would show

       Event  Live Line      Half  ... Time Remaining  Line Variance       Side
2  Green Bay         -4  1st half  ...            0.3            3.0  Favourite

[1 rows x 8 columns]
Reti43
  • 9,656
  • 3
  • 28
  • 44
  • that worked perfectly!!! Thank you! I made that into its own dataframe - any idea how I can text that to my phone? – alty1723 Jan 01 '21 at 22:38
  • If the answer helped you resolve the issue, you are encourage to upvote and/or mark it as accepted. You might want to wait for a bit, in case more answers flow in. In response to sending a message, your `client.messages.create()` call should do the trick. Just make sure the body is a string, which you can create using [string formatting](https://stackoverflow.com/questions/517355/string-formatting-in-python). If you still have difficulties about that, you should ask a new question, as each one should be about one specific problem only. – Reti43 Jan 01 '21 at 22:45
  • I think your `&` has to be replaced with `and` – TheEagle Jan 01 '21 at 22:52
  • 1
    @frederic Actually, in this case `and` won't work and will throw a ValueError. I had forgotten that, because I'm used to use `&` in such situations. – Reti43 Jan 01 '21 at 23:04