I would like to get the column "N_CLAIMS" as an aggregation of "MONTHS_N_INCIDENT" but if it's > 0 count as 1
what should be the optimal way for many columns and rows instead to iterate over the dataframe
CodePudding user response:
I guess aggregation is sum? But in anycase you can do your aggregation across the columns and check if > 0 and cast it to int as you want 0 and 1 not True and False. So something like
columns = ["col1", ...] # if you only need some columns
df["newcolumn"] = (df[colums] > 0).sum(axis=1).astype(int)

