oscarafone oscarafone - 1 year ago 69
Python Question

Apply same selection (cut) to multiple dataframes

My question is about making selections in


As you know, one can apply a
(or 'cut') to a dataframe by doing

df = df[df.area > 10]

if you wanted to (say) select all rows whose column value of
was greater than
. But suppose you have many dataframes, and you'd like to eventually apply this cut to all of them. It would be nice to do something like

cut = dataframe.area > 10

and then somehow be able to do

df = df[cut]

Obviously given the strategy above it won't work because
refers to a specific dataframe. But is there a way to approximate this behavior?

That is, is it possible to define a
that refers to no dataframe in particular and can be applied as
df = df[cut]

Answer Source

I can get something similar

cut = lambda df: df[df.area > 10]

Per @root

cut = 'area > 10'

Per @ayhan

cut = lambda x: x.area > 10


100 rows

df = pd.DataFrame(np.random.randint(0, 20, 100), columns=['area'])

enter image description here

1,000,000 rows

df = pd.DataFrame(np.random.randint(0, 20, 1000000), columns=['area'])

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download