1

This is simple request in python where I need to update 0 where the value in none in one fo the column in mydataframe. I have used following code to do it

test['WIN_AMOUNT_BF_FEATURE']=test['WIN_AMOUNT_BF_FEATURE'].fillna(0)

This works great, however I get a warning

C:\WINDOWS\TEMP/ipykernel_11784/720693040.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy test['WIN_AMOUNT_BF_FEATURE']=test['WIN_AMOUNT_BF_FEATURE'].fillna(0)

I understand using loc will make this process twice faster here. Just not sure how to do this update with the help of loc. I tried following however thats definetly stupid guess here as it didn't work. Please how can I do this with the help of

> loc.

test.loc[test['WIN_AMOUNT_BF_FEATURE']] =test.loc[test['WIN_AMOUNT_BF_FEATURE']].fillna(0) this one didn't work.
  • Can you try this - `test.loc[:, 'WIN_AMOUNT_BF_FEATURE']=test.loc[:, 'WIN_AMOUNT_BF_FEATURE'].fillna(0)` ? – Sajan Mar 18 '22 at 12:03
  • the first one is the good way. the warning you get is because how you created the dataframe `test` before. is it a part of a bigger dataframe? how do you get this dataframe? – Ben.T Mar 18 '22 at 12:16
  • you can read the section **Just tell me how to suppress the warning!** in [this answer](https://stackoverflow.com/a/53954986/9274732) – Ben.T Mar 18 '22 at 12:20
  • Yes, this is part of bigger dataframe, however loc take less time compare to what I have done here. – deepankar srigyan Mar 18 '22 at 12:46

1 Answers1

1

please try this:

test.loc[test['WIN_AMOUNT_BF_FEATURE'].isna()==True, ['WIN_AMOUNT_BF_FEATURE']] = 0
prahasanam_boi
  • 816
  • 3
  • 10