jscriptor
jscriptor

Reputation: 835

Pandas Dataframe replace values in a Series

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.

Upvotes: 4

Views: 580

Answers (3)

jpp
jpp

Reputation: 164623

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

Upvotes: 1

jezrael
jezrael

Reputation: 862511

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

Upvotes: 2

BENY
BENY

Reputation: 323226

You need to assign it back

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

Upvotes: 2

Related Questions