Maverick Meerkat
Maverick Meerkat

Reputation: 6404

Filling nan with increasing sequence in Pandas

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.

Upvotes: 0

Views: 173

Answers (1)

jezrael
jezrael

Reputation: 862671

Use Series.mask with specified columns name:

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

Upvotes: 2

Related Questions