Home > Mobile >  Best Practice to deal with Apply on Pandas when the Dataframe is empty
Best Practice to deal with Apply on Pandas when the Dataframe is empty

Time:09-26

A common pattern I follow in my code is creating a function which will be applied row-wise and return a single value. For example:

def apply_function(row):
   if row["A"] == "valueA":
      return "foo"
   elif row["B"] == "valueB":
      return "bar"
   else: 
      return "baz"

df["applied_column"] = df.apply(apply_function, axis=1)

This is fine as long as df is not empty. But if is, .apply returns instead a DataFrame and I get an exception, so I usually follow this boilerplate code:

if not df.empty:
    df["applied_column"] = df.apply(apply_function, axis=1)
else:
    df["applied_column"] = None 

This works but I feel is not very elegant and not a good practice. Is it indeed the stardard way of dealing with this situation of Pandas, or there is better practices to achieve it?

CodePudding user response:

You can explicitly set return_type so Pandas does not infer anything from the return value of your apply_function.

E.g.

res = df.apply(apply_function, axis=1, result_type="reduce")

always returns a Series.

  • Related