Home > Blockchain >  How can I incorporate an error handling message into dataframe conditionals?
How can I incorporate an error handling message into dataframe conditionals?

Time:01-17

I have a dataframe consisting of two columns:

     trade           venue
      '1'            'TSE'
      '1'            'LSE'
      '3'            'ABC'
      '3'            'TTT'

How can I write a warning/error message in Python such that if trade == 3 and venue is NOT either ABC or BBB, then raise an error message, either stopping the code from running or not stopping the code from running- either is fine I just want to warn the user.

I can write dataframe conditionals but I am struggling to incorporate the error message part into it.

CodePudding user response:

Here is the solution.

import pandas as pd

data = {'a': [3,1,3,1],'b': ['TSE','LSE','ABC','TTT']} #e.x
df = pd.DataFrame(data) #creating df

temp = df[(df.a==3) & (df.b=='TSE')].head(1) #condition for 1 row

def func(): #raising by functin
    raise Exception("Nop")

if len(temp): #length should be equal 1 in that case
    func()
  • Related