4

I am trying to update my_df based on conditional selection as in:

my_df[my_df['group'] == 'A']['rank'].fillna('A+')

However, this is not persistence ... e.g: the my_df still have NaN or NaT ... and I am not sure how to do this in_place. Please advise on how to persist the the update to my_df.

1
  • From the answers below, you have two problems. First, is that you are not re-assign your results back to the dataframe. And, two you have index chaining going on. Index chaining in this situation can be avoid by using loc. Commented Aug 15, 2018 at 14:32

3 Answers 3

2

Create boolean mask and assign to filtered column rank:

my_df = pd.DataFrame({'group':list('AAAABC'),
                     'rank':['a','b',np.nan, np.nan, 'c',np.nan],
                     'C':[7,8,9,4,2,3]})

print (my_df)
  group rank  C
0     A    a  7
1     A    b  8
2     A  NaN  9
3     A  NaN  4
4     B    c  2
5     C  NaN  3

m = my_df['group'] == 'A'
my_df.loc[m, 'rank'] = my_df.loc[m, 'rank'].fillna('A+')

print(my_df)
  group rank  C
0     A    a  7
1     A    b  8
2     A   A+  9
3     A   A+  4
4     B    c  2
5     C  NaN  3
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @jscriptor, I recommend accept this answer , since the clean code is equally important with logic . (Always using mask for the condition is good for future modify )
Thanks a lot. All answers worked but this breaking up into 2 ops help me see what I was doing wrong.
2

You need to assign it back

my_df.loc[my_df['group'] == 'A','rank']=my_df.loc[my_df['group'] == 'A','rank'].fillna('A+')

Comments

1

Your operations are not in-place, so you need to assign back to a variable. In addition, chained indexing is not recommended.

One option is pd.Series.mask with a Boolean series:

# data from @jezrael

df['rank'].mask((df['group'] == 'A') & df['rank'].isnull(), 'A+', inplace=True)

print(df)

   C group rank
0  7     A    a
1  8     A    b
2  9     A   A+
3  4     A   A+
4  2     B    c
5  3     C  NaN

Comments

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.