Home > Blockchain >  Pandas add new column as "agregation" of different columns but counting them as 1 in case
Pandas add new column as "agregation" of different columns but counting them as 1 in case

Time:01-16

I would like to get the column "N_CLAIMS" as an aggregation of "MONTHS_N_INCIDENT" but if it's > 0 count as 1

enter image description here

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)
  •  Tags:  
  • Related