0

I have a dataframe which I'm trying to replace all NaN's with the word "Unknown".

I tried using the following code:

reviews[reviews.country.fillna("Unknown")]

This does not work.

What will work, is the code below which to my knowledge creates a series:

reviews.country.fillna("Unknown")

But, my question is why can I not create a dataframe with the fillna function; for some reason, I cannot apply "reviews[]" around the entire code?

Why will fillna only work for a series?

To make things even more confusing for me, I can make a dataframe with the isnull() function:

reviews[reviews.country.isnull()]

As you can see, here I can apply "reviews[]" around the entire code.

Why won't fillna not handle the same?

Can anyone explain to me the concept of what's happening?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
mikelowry
  • 1,307
  • 4
  • 21
  • 43
  • 1
    `reviews[reviews.country.fillna("Unknown")]` tries to slice the Index by this Series with filled values. This is clearly not what you want to do. – ALollz Aug 26 '19 at 20:28
  • I suggest you review https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html#selection of the 10 minutes to pandas tutorial. Your second selection can be found specifically in the section https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html#boolean-indexing – ALollz Aug 26 '19 at 20:29

1 Answers1

0

So you can apply fillna() to the entire dataframe OR to one Serie (a column).

## 1. Turns all NaN to "Unknown"
reviews.fillna(value="Unkown", inplace=True)
# Or, reviews = reviews.fillna("Unkown")

## 2. Turns all NaN from the column 'country' to "Unknown"
reviews['country'].fillna(value="Unkown", inplace=True)
# Or, reviews.country.fillna(value="Unkown", inplace=True)
# Or, reviews['country'] = reviews['country'].fillna("Unknown")
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
Benoit Drogou
  • 969
  • 1
  • 5
  • 15