0

I have a following date frame.

mydf = pd.DataFrame({'month':[2,3,7,8],'day':[29,24,20,29]})
>>> mydf
   day  month
0   29      2
1   24      3
2   20      7
3   29      8

>>> leapdf =  mydf.loc[(mydf['month'] == 2) & (mydf['day'] == 29) ]
>>> leapdf
   day  month
0   29      2

>>> otherdf =  mydf.loc[(mydf['month'] != 2) & (mydf['day'] != 29) ]
>>> otherdf
   day  month
1   24      3
2   20      7

Above otherdf I am expecting day 29 and month 8 to be included. But not included. Can any one guide me what is the error.

Thanks

2
  • Why would you expect day 29 to be included when you explicitly denied it? (mydf['day'] != 29). Your filter removes all months of Feb and all days (regardless of month) equal to day 29. Commented Aug 9, 2017 at 6:02
  • thanks got the mistake Commented Aug 9, 2017 at 6:06

1 Answer 1

1

You need | for or:

otherdf =  mydf.loc[(mydf['month'] != 2) | (mydf['day'] != 29) ]

What is same as:

otherdf =  mydf.loc[~((mydf['month'] == 2) & (mydf['day'] == 29)) ]
print (otherdf)
   day  month
1   24      3
2   20      7
3   29      8
Sign up to request clarification or add additional context in comments.

Comments

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.