Skip to content

ENH: df.grep(col,pat) and df.dselect(col,"expr") #2460

Closed
@ghost

Description

wes, how would you feel about adding something like the following as
a Dataframe method? especially with method chaining I would find this
useful. Will add handling for datatypes and so on.

In [32]: from pandas.util.testing import makeCustomDataframe as mkdf
In [33]: def grep(df,col,pat):
    ...:     vals=df[col]
    ...:     return df.ix[[bool(re.search(pat,unicode(x))) for x in vals]]
In [34]: df=mkdf(20,2)
    ...: print df
C0       C_l0_g0 C_l0_g1  \
R0                         
R_l0_g0     R0C0    R0C1   
R_l0_g1     R1C0    R1C1   
R_l0_g10    R2C0    R2C1   
R_l0_g11    R3C0    R3C1   
R_l0_g12    R4C0    R4C1   
R_l0_g13    R5C0    R5C1   
R_l0_g14    R6C0    R6C1   
R_l0_g15    R7C0    R7C1   
R_l0_g16    R8C0    R8C1   
R_l0_g17    R9C0    R9C1   
R_l0_g18   R10C0   R10C1   
R_l0_g19   R11C0   R11C1   
R_l0_g2    R12C0   R12C1   
R_l0_g20   R13C0   R13C1   
R_l0_g3    R14C0   R14C1   
R_l0_g4    R15C0   R15C1   
R_l0_g5    R16C0   R16C1   
R_l0_g6    R17C0   R17C1   
R_l0_g7    R18C0   R18C1   
R_l0_g8    R19C0   R19C1   

In [35]: grep(df,"C_l0_g0","R.*6")
Out[35]: 
C0       C_l0_g0 C_l0_g1  \
R0                         
R_l0_g14    R6C0    R6C1   
R_l0_g5    R16C0   R16C1  

Metadata

Metadata

Assignees

No one assigned

    Labels

    IdeasLong-Term Enhancement Discussions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions