2

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
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
ProjektWeinheim
  • 201
  • 3
  • 10

3 Answers3

3

One way I could think of is, instead of using: df['A'] == 'department' you can use df['A'].isin(['department']). The two yield the same result.

Once you do that, then you can pass the "Take All" parameter like so:

df['A'].isin(df['A'].unique())

where df['A'].unique() is a list all the unique paratemres in this column, so it will return all True.

Or you can pass multiple parameters like so:

df['A'].isin(['department', 'string_2', 'string_3']))
Newskooler
  • 3,973
  • 7
  • 46
  • 84
1

I don't think you can do it by passing a parameter like in an SQL query. You'll have to re-write your function a bit to take this condition into consideration.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32
1

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.

jaumebonet
  • 2,096
  • 1
  • 15
  • 18