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