0

I have a pandas dataframe as

my_df = pd.DataFrame({"months":[0,1,2,3,4,5], "value":[12,123,np.nan,234,345,456]})

I wanted to check for specific months(such as 0, 1, 3) any value is null or 0

I tried to do following way but does not work

if my_df[(my_df["months"].isin([0, 1, 3])) & (my_df["value"].isnull() | my_df["value"] == 0)].empty:

this still gives output as True

1
  • I want to check value for months 0,1,3 is not Nan or 0 Commented Jul 21, 2021 at 17:09

1 Answer 1

1

You forgot to add the "not" ~ part while checking for null values. Try this instead:

>>> my_df[(my_df["months"].isin([0,1,3])) & ~(my_df["value"].isnull()|my_df["value"]==0)]
   months  value
0       0   12.0
1       1  123.0
3       3  234.0

With your empty check:

>>> my_df[(my_df["months"].isin([0,1,3])) & ~(my_df["value"].isnull()|my_df["value"]==0)].empty
False

Edit:

To check if any value for the given months is not zero or null, you could do:

>>> any((my_df["months"].isin([0,1,2]))&((my_df["value"].isnull())|(my_df["value"]==0)))
True
Sign up to request clarification or add additional context in comments.

4 Comments

but if I change months and add 2 it give me the wrong output as False again >>> my_df[(my_df["months"].isin([0, 1, 2])) & ~(my_df["value"].isnull()|my_df["value"]==0)].empty
The output should be False. You have rows (0, 12) and (1, 123) which have months in [0, 1, 2] where the values are not null. What do you expect?
Oh, apologies (Changed the question removed not), I mean to say if any of given months have null or 0 values then I need to log them in error
@RachanaMahajan - Edited

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.