2

I am trying to filter out rows from my data:

 cid    date    catcode     amtsum
145403  N00000286   2009    F1100   0.500
199228  N00000286   2009    Z5100   4.000
485489  N00000286   2007    B4000   3.300
485547  N00000286   2007    F5100   5.000
488556  N00000286   2007    E4100   2.500
490622  N00000286   2007    F1400   5.000
490924  N00000286   2007    T3100   1.000
490957  N00000286   2007    K1200   5.000
495039  N00000286   2007    Z5300   0.051
496078  N00000286   2008    K1000   13.100

Here is some of my code:

#This is data for Barack Obama that I do not want in my data frame. The 'cid' code identifies Obama, I want to remove Obama for the years specified by 'date'.
mask = (campaign_contributions['cid'] == 'N00009638') & (campaign_contributions['date'] >= 2007) 
campaign_contributions = campaign_contributions[~mask]

#This is data for John McCain that I do not want in my data frame. The 'cid' code identifies McCain, I want to remove McCain for the years specified by 'date'.
mask1 = (campaign_contributions['cid'] == 'N00006424') & (campaign_contributions['date'] == 2008) & (campaign_contributions['date'] == 2007) 
campaign_contributions = campaign_contributions[~mask1]

#This is data for Bob Barr that I do not want in my data frame. The 'cid' code identifies Barr, I want to remove Barr for the years specified by 'date'.
mask2 = (campaign_contributions['cid'] == 'N00002526') & (campaign_contributions['date'] == 2008) & (campaign_contributions['date'] == 2007) 
campaign_contributions = campaign_contributions[~mask2]

#This is data for Ralph Nader that I do not want in my data frame.The 'cid' code identifies Nader, I want to remove Nader for the years specified by 'date'.
mask3 = (campaign_contributions['cid'] == 'N00000086') & (campaign_contributions['date'] == 2008) & (campaign_contributions['date'] == 2007)
campaign_contributions = campaign_contributions[~mask3]

The code above represents the rows I want to filter out. I think that I am using the ~mask tool improperly. Ideally, my end project will be the data frame without the rows specified above, i.e. I don't want this information in my data frame:

Can someone steer me in the right direction on this?

Collective Action
  • 7,607
  • 15
  • 45
  • 60
  • I think you're using it wrong too. It's difficult to use wrong code to deduce your intent. Could you verbalize what you're trying to do as well? – piRSquared Jul 29 '16 at 19:26

1 Answers1

4

You can use the bitwise and operator & to combine masks. It might look something like this:

campaign_contributions = campaign_contributions[~mask & ~mask1 & ~mask2 & ~mask3]

Alternatively you could also use the or operator | to do:

campaign_contributions = campaign_contributions[~(mask | mask1 | mask2 | mask3)]

You can find more information in this post.

Community
  • 1
  • 1