Suppose I have a groupby
object, a DataFrame, or anything else with an apply()
method. I want some elements to not map to any output. For example, in my case I have a groupby
and I want groups that satisfy a certain criteria to be ignored. How can I do that? I've tried return None
in the function being applied, but the output still has an entry for the group (it's null but it's still there).
For example, suppose a DataFrame looks like this:
good_row 272.0 42440.0 29893408.0
good_row_2 142.0 22360.0 12965953.0
bad_row 171.0 26920.0 14726556.0
I want to run df.apply(fn, axis=1)
such that for good rows, fn returns some output, and for the bad row, fn tells apply to "ignore" the row, and the output does not have an entry for bad_row
. Here I used a DataFrame rather than a groupby for ease of demonstration but it's the same idea.