Anirban Chakraborty
Anirban Chakraborty

Reputation: 781

How to select rows from a series based on values or condition, dynamically?

I have a series produced dynamically, say - df['col1'].value_counts(). I can save this as a series s and then select rows from the series, for e.g., -

s = df['col1'].value_counts()
s_selected = s[s==20]

However I would like to achieve the same dynamically, something like - df['col1'].value_counts().some_function(filtering_condition)

Dataframe has an API like this - df.query()

How to do the same for Series.

Upvotes: 0

Views: 439

Answers (1)

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10970

Use Series.where chained with dropna

s.where(lambda x: x == 20).dropna()

Upvotes: 1

Related Questions