Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replacing nulls in a column range

So I am editing a dataframe for a project and I need to replace null values in 105 columns with ‘No answer’
in order to do this I wrote the following code but it only created a view of the updated dataframe. when I look at the actual dataframe nothing has actually changed. I find this odd because im using loc method and fillna('No answer',interface=True). I thought by making interface = True it meant the actual dataframe would be updated and the same for loc function.

Code I used:

candy.loc[:,'Q6 | 100 Grand Bar':'Q11: DAY'].fillna('NO_ANSWER', inplace=True)
candy.loc[:,'Q6 | 100 Grand Bar':'Q11: DAY']

Second code just prints out the original dataframe for the columns I wanted to edited
You can see in the image that the Nan values are still there after running first code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can any one explain my error in logic and why its not working as intended
and possible solutions. Pic below is what Im getting
enter image description here

>Solution :

When you are using .loc, you are generating a new object, which is a slice of the original dataframe. inplace=True is applied to this new object, not the original dataframe, hence you see no changes in the original dataframe. So what you want to do is the following:

candy.loc[:,'Q6 | 100 Grand Bar':'Q11: DAY'] = candy.loc[:,'Q6 | 100 Grand Bar':'Q11: DAY'].fillna('NO_ANSWER')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading