How to raise Error message if any rows contains negative Values

shourya hinger

I have a dataframe as shown below

Dataframe-df

enter image description here

First I have to filter out the Columns containing names starting with V
Then I have to check in the filtered dataframe if any rows contains negative Values and raise and error for the rows containing negative values
Second I have to raise a warning message for the non filtered dataframe(W) if it contains any negative values
I have used the below code to separate out the columns starting with V

df1=df.filter(regex='V')

df2=df1[df<0]

Please help how to proceed further.

pansen

Here is some dummy data to work with in this example:

import pandas as pd
import numpy as np

# create dummy data
numbers = np.random.randint(-100, 100, size=(5, 4))

# save as temporary file for timeit
df = pd.DataFrame(numbers, columns=["V1", "V2", "W1", "W2"])

print(df)

   V1  V2  W1  W2
0  53  88  36 -33
1  -1 -85  60  -8
2  27  34 -11  61
3  92  54 -76 -37
4 -16 -39  13 -81

Basically, you need two steps here. First, filter the relevant columns for which you've already provided a solution. Just a short note here, you may use the like parameter which is more appropriate in this example because you don't need the entire power of regular expressions.

Second, you need a function which filters all rows which contain any values below zero. This is exactly what the filter_negatives function does below. In particular, the lt(0) returns True for all values lower than 0 and any(axis=1) returns True for all row indices for which any value in a row (axis=1) is True.

def filter_negatives(sub_df):
    bool_series = sub_df.lt(0).any(axis=1)  
    return sub_df.index[bool_series]

neg_v_rows = filter_negatives(df.filter(like="V"))
neg_w_rows = filter_negatives(df.filter(like="W"))

print(neg_v_rows)
Int64Index([1, 4], dtype='int64')

print(neg_w_rows)
Int64Index([0, 1, 2, 3, 4], dtype='int64')

Finally, you can raise errors or create warnings based on neg_v_rows or neg_w_rows. Because I'm not sure how you want to raise errors or return warnings here, I skip this for now. Raising an error in Python will ultimately break your program (if not caught) and your dummy table above contains values below zero in almost all rows. Please specify if required.

To check if your unfiltered data frame contains any negative values at all, you can simply use neg_w_rows.any().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How raise value error of "n must not be negative"

How to cause an error message when a negative number is inputted?

How To Raise Exception with Message in Ada

How to identify error with scipy.stats.chisquare returns negative values?

how to find rows with both positive and negative values in pandas dataframe

How to extract same rows with positive and negative values

How to drop all rows in pandas dataframe with negative values?

How to select rows with only positive or negative values in Pandas

Error when trying to raise a discrete random variable to a negative power

custom validator for charfield do not raise any error

Select rows with at least one (any) negative value

How to hightlight rows in Excel 2013 that contains duplicate values?

How to identify if a cell contains any of the values in a column

How to check if a list contains a group of values in any order - Python

How to filter dataframe rows if column value (string) contains any of the values in a set in python?

How to test for negative number and raise ValueError?

How to raise an Error for the completionHandler?

Raise custom error message for

Google BigQuery: How to check if Year string contains negative values

How do I filter only those rows which contains any of the values from a given list of tags

How to check if element in array contains any values from a list Python

How to iterate through rows and get max values of any previous rows

How to extract only error message from Oracle raise_application_error in C#

How to replace only a column and for the rows contains specific values

How to raise a message box in case of not handled Error in Python?

How to set quantity to 0 but not negative value in my code? if its shows negative value then put an error message in alert

In Sql Alchemy, how to select rows where any column contains a substring?

How can I drop all rows in a group if any column contains only nan values for that group?

Shell script raise error if any command fails