Home > Net >  Filling nan with increasing sequence in Pandas
Filling nan with increasing sequence in Pandas

Time:10-25

I try to translate this R code to pandas:

data1 = data1 %>% mutate(id = ifelse(is.na(id), cumsum(is.na(id)), id))

What it does - it looks for missing ID's and it gives them a unique number that is increasing (from 1 to ...).

I tried doing:

df.assign(id = lambda x: x.isnull().cumsum() if x.isnull() else x) but I keep getting errors.

CodePudding user response:

Use Series.mask with specified columns name:

df.assign(id = lambda x: x['id'].mask(x['id'].isnull(), x['id'].isnull().cumsum()))
  • Related