-1

I have a big dataset, where in one of the columns I want to replace all the NaN values by "ZZZ". I have already done this:

df['code_diag_secund_icd10'].fillna("ZZZ", inplace = True)

However, when I do

df['code_diag_secund_icd10'].isnull().sum()

It gives me the same amount of nan values as before.

Anyone can help me, please?

AMC
  • 2,466
  • 7
  • 11
  • 31
bonaqua
  • 79
  • 6

2 Answers2

0

Try following solution: Pandas Replace NaN with blank/empty string

In your case just paste desired string instead of empty string.

Alex Bodnya
  • 124
  • 10
  • If this question is a duplicate, then you should flag it as such, no? – AMC Feb 26 '20 at 17:34
  • I did df['code_diag_secund_icd9'].replace('ZZZ', np.nan, regex=True), but it still does not replace them. It is as if it did not record the new dataframe with the new values – bonaqua Feb 26 '20 at 17:44
0

you can use replace function to replace string and coming to find nan value np.nan will find the nan value and replace with ZZZ

import numpy as np
df = df.replace(np.nan, 'ZZZ', regex=True)
Jay Kakadiya
  • 413
  • 1
  • 4
  • 12
  • 1
    Please edit your answer with a textual explanation of the code snippet and explain how it answers the question – chevybow Feb 26 '20 at 22:53
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – colidyre Feb 27 '20 at 00:37
  • Sure let me add explanation – Jay Kakadiya Feb 27 '20 at 00:39