serious_opinions
serious_opinions

Reputation: 1

how to pass regex as function parameter

I have following python function which takes query which is string as an input but instead of just passing simple string I would like pass it as regex...how can I do it. here query is string which contains alphabet/number/symbol for example "MSEB-1020-MSK-01" and "MSEB-102*-MSK-**"

def function(string: str):

    f_df = df_.loc[
        df_["Company"].str.contains(string)
    ]

Upvotes: 0

Views: 235

Answers (1)

Echo
Echo

Reputation: 293

Try this:

def function(df, string):

    f_df = df.loc[df["company"].str.contains(query, regex=True)] # `regex = True`, allows you to pass regex expresion

    return f_df  

df_query = function(df_benchmark, "whatever_your_regex_is")

hope this works for you

Upvotes: 1

Related Questions