Skip to content

Drop If syntax #2064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jseabold opened this issue Oct 12, 2012 · 6 comments
Closed

Drop If syntax #2064

jseabold opened this issue Oct 12, 2012 · 6 comments
Labels
Ideas Long-Term Enhancement Discussions

Comments

@jseabold
Copy link
Contributor

Maybe there's already a way to do this, and there's certainly a safer and more robust/full-featured way to do this, but I just monkey-patched my DataFrames to have a dropif method because I got tired of typing.

def dropif(self, col, cond):
    return self.ix[~eval("self['%s']"%col +cond)]

In the spirit of Stata's drop and keep commands.

@lodagro
Copy link
Contributor

lodagro commented Oct 15, 2012

df.drop(df.index[boolean_mask]), is what i use.

In [82]: df = pd.DataFrame(np.random.randn(10,5), columns=list('ABCDE'))

In [83]: df
Out[83]: 
          A         B         C         D         E
0 -1.069739 -1.736850  1.883675 -1.002858  1.055439
1 -0.847116 -0.151453  0.973839  1.183713 -0.466413
2 -1.068765  1.070346  0.109815  0.327656  0.749683
3  1.302732  0.641277  0.212475  1.194309 -0.224311
4  1.411065  0.230903  1.574795  1.823085 -0.244319
5  1.020547  0.233539 -1.386656 -0.511132  0.436428
6 -0.855869 -0.130812 -1.397438 -0.785379  0.823477
7  0.178139  2.027483 -0.810593 -1.196736  0.192493
8  0.341994  0.844972 -0.245320 -2.516069 -1.703321
9  0.698949  0.290125 -0.693364 -2.742538  1.007149

In [84]: df.drop(df.index[(df.A > 0) & (df.E < 0)])
Out[84]: 
          A         B         C         D         E
0 -1.069739 -1.736850  1.883675 -1.002858  1.055439
1 -0.847116 -0.151453  0.973839  1.183713 -0.466413
2 -1.068765  1.070346  0.109815  0.327656  0.749683
5  1.020547  0.233539 -1.386656 -0.511132  0.436428
6 -0.855869 -0.130812 -1.397438 -0.785379  0.823477
7  0.178139  2.027483 -0.810593 -1.196736  0.192493
9  0.698949  0.290125 -0.693364 -2.742538  1.007149

@paulproteus
Copy link

Hi @lodagro and @jseabold ,

Is the right conclusion that pandas should close this ticket?

Or should, instead, we keep the ticket open, and suggest that a contributor write the small bit of code it would take to support @jseabold 's request?

It seems somewhat difficult to support this use case cleanly, so best to close the ticket in my opinion. But I thought I'd ping the ticket and see what you all think.

If we believe the issue can be fixed by adding a note in the documentation, that could be a good task for contributors.

@wesm
Copy link
Member

wesm commented Dec 16, 2012

Let's leave it open. Fruitful fodder for future design discussions (e.g. more eval? as in "more cowbell")

@petehuang
Copy link
Contributor

A dropif functionality was presented in 0.13.0 via DataFrame.query. Should we close this issue?

Also tagging in @jreback for a more currently active maintainer:

@bashtage
Copy link
Contributor

Is this still relevant?

@jreback
Copy link
Contributor

jreback commented Oct 17, 2017

pretty idiomatic

In [15]: df.loc[~((df.A > 0) & (df.E < 0))]

@jreback jreback closed this as completed Oct 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ideas Long-Term Enhancement Discussions
Projects
None yet
Development

No branches or pull requests

7 participants