Search code examples
pythonpandaspandas-loc

How to pass a "Take All" parameter in pandas loc filter condition?


I have a function with a parameter (in this case: "department") to filter (df.loc[(df['A'] == department) specific data out of my dataset. In one case, I want to use this specific function but instead of filtering the data, I want to get all the data.

Is there a way to pass a parameter which would result in something like df.loc[(df['A'] == *) or df.loc[(df['A'] == %)

    # Write the data to the table 

    def table_creation(table, department, status):

        def condition_to_value(df, kpi):
            performance_indicator = df.loc[(df['A'] == department) & (df['C'] == kpi) & (df['B'] == status), 'D'].values[0]
        return performance_indicator

Solution

  • Building over Newskooler's answer, as you know the name of the column you'll be searching over, you could add his solution inside the function and process '*' accordingly.

    It would look something like this:

    # Write the data to the table 
    def table_creation(table, department, status):
        def condition_to_value(df, kpi):
            # use '*' to identify all departments
            if isinstance(department, str) and department=='*':
                department = df['A'].isin(df['A'].unique()) 
            # make the function work with string or list inputs
            if isinstance(department, str):
                department = [department, ]
            # notice the addition of the isin as recommended by Newskooler
            performance_indicator = df.loc[(df['A'].isin(department)) & (df['C'] == kpi) & (df['B'] == status), 'D'].values[0]
            return performance_indicator
    

    I realize there are missing parts here, as they are also in the initial question, but this changes should work without having to change how you call your function now, but will include the benefits listed in the previous answer.