What is the most pythonic way to write a function which does a row-wise aggregation (sum, min, max, mean etc) of a specified set of columns (column names in a list) of a pandas dataframe while skipping NaN values?
import pandas as pd
import numpy as np
df = pd.DataFrame({"col1": [1, np.NaN, 1],
"col2": [2, 2, np.NaN]})
def aggregate_rows(df, column_list, func):
# Check if the specified columns exist in the DataFrame
missing_columns = [col for col in column_list if col not in df.columns]
if missing_columns:
raise ValueError(f"Columns not found in DataFrame: {missing_columns}")
# Check if func is callable
if not callable(func):
raise ValueError("The provided function is not callable.")
# Sum the specified columns
agg_series = df[column_list].apply(lambda row: func(row.dropna()), axis=1)
return agg_series
df["sum"] = aggregate_rows(df, ["col1", "col2"], sum)
df["max"] = aggregate_rows(df, ["col1", "col2"], max)
df["mean"] = aggregate_rows(df, ["col1", "col2"], lambda x: x.mean())
print(df)
results in (as expected):
col1 col2 sum max mean
0 1.0 2.0 3.0 2.0 1.5
1 NaN 2.0 2.0 2.0 2.0
2 1.0 NaN 1.0 1.0 1.0
but a row with only NaN values,
df = pd.DataFrame({"col1": [1, np.NaN, 1, np.NaN],
"col2": [2, 2, np.NaN, np.NaN]})
results in:
ValueError: max() arg is an empty sequence
What is the best way to fix this?
You can try to use
numpy.sum/numpy.max/numpy.meaninstead of Python's builtins:Prints: