pandas: filter rows of DataFrame with operator chaining pandas: filter rows of DataFrame with operator chaining python python

pandas: filter rows of DataFrame with operator chaining


I'm not entirely sure what you want, and your last line of code does not help either, but anyway:

"Chained" filtering is done by "chaining" the criteria in the boolean index.

In [96]: dfOut[96]:   A  B  C  Da  1  4  9  1b  4  5  0  2c  5  5  1  0d  1  3  9  6In [99]: df[(df.A == 1) & (df.D == 6)]Out[99]:   A  B  C  Dd  1  3  9  6

If you want to chain methods, you can add your own mask method and use that one.

In [90]: def mask(df, key, value):   ....:     return df[df[key] == value]   ....:In [92]: pandas.DataFrame.mask = maskIn [93]: df = pandas.DataFrame(np.random.randint(0, 10, (4,4)), index=list('abcd'), columns=list('ABCD'))In [95]: df.ix['d','A'] = df.ix['a', 'A']In [96]: dfOut[96]:   A  B  C  Da  1  4  9  1b  4  5  0  2c  5  5  1  0d  1  3  9  6In [97]: df.mask('A', 1)Out[97]:   A  B  C  Da  1  4  9  1d  1  3  9  6In [98]: df.mask('A', 1).mask('D', 6)Out[98]:   A  B  C  Dd  1  3  9  6


Filters can be chained using a Pandas query:

df = pd.DataFrame(np.random.randn(30, 3), columns=['a','b','c'])df_filtered = df.query('a > 0').query('0 < b < 2')

Filters can also be combined in a single query:

df_filtered = df.query('a > 0 and 0 < b < 2')


The answer from @lodagro is great. I would extend it by generalizing the mask function as:

def mask(df, f):  return df[f(df)]

Then you can do stuff like:

df.mask(lambda x: x[0] < 0).mask(lambda x: x[1] > 0)


matomo