I have a function that takes an indexed pandas.Series of things and a dataframe of stuff that I want to use the things on group by group. It is common for the dataframe to contain groups for which there is no matching thing, so a simple list comprehension will often throw exceptions.

My Python is pretty rusty. Is code like this considered normal? It strikes me that it might be replicating some library function that I should use instead. Or just that it might be bad practice compared to some other way. My justification for the local function is that it has no use aside from being a helper function for the list comprehension, so why make it global?

def use_things(things, stuff_to_use_things_on):
    stuff_grouped = stuff_to_use_things_on.groupby(things.index.names)

    # find and use the right thing, or else move on
    # local closure since this function has no real use outside this context
    def use_thing(name, group):
        try:
            return things.loc[(name)].do_something(group)
        except:
            return None

    # stuff might contain groups that there is no thing for
    results = [use_thing(name, group) for (name, group) in stuff_grouped]
    
    return pd.concat(results)
1

There are 1 answers

3
PlasmaRaptor360 On

I believe what you are looking for is either the apply_map() or apply() method of dataframes. Documentation for apply() can be found here, while documentation for apply_map() can be found here. If you want to apply a function across all elements of a dataframe, you should use apply_map(). If you want to only apply a function across an axis, use apply(). Keep in mind that these methods cannot be performed in place.