1

Normal validations I am able to do using

 m1 = (df[some_column] == some_value )
 m2 = ( df[some_column].isin(some_list_of_values) )# This check whether the value of the column is one of the values in the list
 m3 = ( df[some_column].str.contains() # You can use it the same as str.contains())
 m4 = (df[some_column].str.isdigit()) # Same usage as str.isdigit(), check whether string is all digits, need to make sure column type is string in advance

Then to get the dataframe after all the above validations-

df = df[m1 & m2 & m3 & m4]

When I print (df[some_column] == some_value ) I get

0 False
1 True
2 True

I want to validate something in a function using if else, like ,

if min_group_price is True , then both single_male single_female needs to be True
If min_group_price is False , then no check(Final result should be True)

My test data is something like ,

min_group_price single_male single_female 
0 1.0 2.0 3.0 
1 NaN NaN NaN 
2 1.0 2.0 NaN 
3 NaN 2.0 NaN 
4 0.0 NaN 4.0 
5 NaN NaN 2.0

In this as per the above logic, index 0,1,3,5 should be True. I dont want to iterrows . How can I do this?

1 Answer 1

1

You've just described some boolean logic which is easy to implement with pandas:

(~df['min_group_price'].notna()) | (
    df['single_male'].notna() & df['single_female'].notna())

0     True
1     True
2    False
3     True
4    False
5     True
dtype: bool

If 'min_group_price' is not null, then the result depends on 'single_male' and 'single_female' not being null, otherwise the result is True.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi.. it does solve my issue . But for a long validations like the one I have just updated in the question, how would we do that ?
@user1896796 you already have an answer to your original problem as stated. If you are unable to apply the answer to your own problem, please don't change the nature of the question... open a new one instead. Please at least produce a minimal reproducible example.
@ coldspeed . can you check help in this question.stackoverflow.com/questions/53553873/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.