2

I'm trying to replace my column with NaN

group_choices = ['Group1', 'Group2', 'Group3']

Groups limit
1 NaN NaN
2 Group1 2
3 Group2 2
4 Group3 2
5 NaN NaN
6 NaN NaN
7 NaN NaN

How can I replace NaN, randomly based on the group_choises?

I'm also trying to limit how often a group_choise can be randomised selected because of the limit value in the limit column.

I'm trying to get this result:

Groups limit
1 Group3 NaN
2 Group1 2
3 Group2 2
4 Group3 2
5 Group1 NaN
6 Group2 NaN
7 Out of groups

1 Answer 1

2

fillna with dictionaries

dct = dict(zip(df.Groups.loc[pd.isna].index, group_choices))

df.fillna({'Groups': dct}).fillna({'Groups': 'Out of groups'})

          Groups  limit
1         Group1    NaN
2         Group1    2.0
3         Group2    2.0
4         Group3    2.0
5         Group2    NaN
6         Group3    NaN
7  Out of groups    NaN

Old Answers

Useful but I like the new one better. It illustrates the evolution of my thought process.

Generators

def get_some(i, n):
  for x in [*i] * n:
    yield x

def fill(s, i, n):
  gs = get_some(i, n)
  for x in s:
    if pd.isnull(x):
      try:
        yield next(gs)
      except StopIteration:
        yield "Out of groups"
    else:
      yield x

df.assign(Groups=[*fill(df.Groups, group_choices, 1)])

          Groups  limit
1         Group1    NaN
2         Group1    2.0
3         Group2    2.0
4         Group3    2.0
5         Group2    NaN
6         Group3    NaN
7  Out of groups    NaN

Alternative

def get_some(i, n):
  for x in [*i] * n:
    yield x

df.assign(Groups=df.Groups.fillna(
    df.Groups.loc[pd.isna].pipe(
        lambda s: pd.Series(dict(zip(s.index, get_some(group_choices, 1))))
    )
).fillna('Out of groups'))
Sign up to request clarification or add additional context in comments.

2 Comments

@S.Hendriks I've updated my post again. You may like the new version better.
Wow you are truly amazing, thank you the top one looks sick!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.