0

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.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99

1 Answers1

0

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().

pansen
  • 6,433
  • 4
  • 19
  • 32
  • Thanks it works great @pansen . I want to raise the error to break the code so that i can check the column with negative values – shourya hinger Feb 27 '17 at 17:39
  • @shouryahinger You already know *when* to raise the error. Have a look [here](http://stackoverflow.com/questions/2052390/manually-raising-throwing-an-exception-in-python) for *how* to raise errors. – pansen Feb 28 '17 at 07:38