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)
I believe what you are looking for is either the
apply_map()
orapply()
method of dataframes. Documentation forapply()
can be found here, while documentation forapply_map()
can be found here. If you want to apply a function across all elements of a dataframe, you should useapply_map()
. If you want to only apply a function across an axis, useapply()
. Keep in mind that these methods cannot be performed in place.