SuhailY
SuhailY

Reputation: 69

Creating a filter and wrapping it in a generic function

Assuming I had a tv station dataset, how would I go about creating a generic filter function to filter data from two columns:

  1. column['station_call_name'] should return all names starting with 'K' and 'W'.
  2. column['station_affiliation'] should return values of the following names: ['ABC', 'MBC','BBC']

My ask is to create a generic function that can be applied to any dataset and then implement the specific criteria to the filter function.

Upvotes: 0

Views: 93

Answers (1)

Ynjxsjmh
Ynjxsjmh

Reputation: 30050

If I understand you correctly, you can do

def filter(df):
    m = df['station_call_name'].str.startswith('K|W')
    n = df['station_affiliation'].isin(['ABC', 'MBC','BBC'])
    return df[m&n]

Upvotes: 1

Related Questions