Pratham_Amitabh
Pratham_Amitabh

Reputation: 71

Replacing values using a filter

Let me breakdown what I'm trying to do,

  1. Created a filter for my data-frame, in the column NAME_INCOME_TYPE == Pensioner
  2. Applied filter
  3. Select all NaN values in the column, OCCUPATION_TYPE
  4. Replace NaN with 'Retiree'
  5. Apply to original data-frame

But I keep getting the below error,

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

The code I've written to do the above,

filt = app_data['NAME_INCOME_TYPE'] == 'Pensioner'

app_data.loc[filt]['OCCUPATION_TYPE'].fillna('Retiree', inplace = True)

Data-Frame I'm using enter image description here

Upvotes: 0

Views: 137

Answers (1)

Yevhen Kuzmovych
Yevhen Kuzmovych

Reputation: 12140

I believe using .loc[filter, column].fillna(...) like:

filt = app_data['NAME_INCOME_TYPE'] == 'Pensioner'
app_data.loc[filt,'OCCUPATION_TYPE'] = app_data.loc[filt,'OCCUPATION_TYPE'].fillna('Retiree')

should work.

Upvotes: 1

Related Questions