0

I am trying to replace the Nan values with empty strings. This is the code I have

df.replace({r'[^\x00-\x7F]+': ''}, regex=True, inplace=True)
col_buyername = basicinfo.get('buyer_name')
col_product = basicinfo.get('product_name')
col_quantity = basicinfo.get('quantity')
col_price = basicinfo.get('price')
print(df[col_buyername])

df.loc[:, [col_buyername, col_product]].fillna("", inplace=True)

print('after')
print(df[col_buyername])

the output is

0          NaN
1          Roy Thomas
2          NaN
3          NaN

Name: buyer name, dtype: object

after

0          NaN
1          Roy Thomas
2          NaN
3          NaN

Name: buyer name, dtype: object

Why is the fillna nothing setting it to be blank strings?

6
  • NaN means Not a Number. it is float, not str. You can't put string to a float Commented Sep 17, 2021 at 21:00
  • it is showing as dtype:object?? how can I put blank str there ? Commented Sep 17, 2021 at 21:01
  • @Karina when i did df.fillna('', inplace=True) it was working Commented Sep 17, 2021 at 21:02
  • oh I think this is your mistake: df = df[[col_buyername, col_product, col_address]].fillna("", inplace=True). Add df = and problem is solved (probably) Commented Sep 17, 2021 at 21:07
  • @MichaelO. will this work df[df[[col_buyername, col_product, col_address]].isna()] = "" I got error TypeError: Cannot do inplace boolean setting on mixed-types with a non np.nan value Commented Sep 17, 2021 at 21:09

1 Answer 1

3

Accessing with square brackets and a list of columns creates a copy, so you modify a temporary object, not the original dataframe.

You have three possible solutions, either pass a dict of column -> replacement for each column, assign or loop over the columns.

Looping

for col in (col_buyername, col_product):
    df[col].fillna('', inplace=True)

Assignment

df[[col_buyername, col_product]] = df[[col_buyername, col_product]].fillna('')

dict

df.fillna({col_buyername: '', col_product: ''}, inplace=True)

The loop and the dict approach should be a little more efficient than the reassignment.

For more info on when pandas created copies and when not, see https://stackoverflow.com/a/53954986/3838691

Sign up to request clarification or add additional context in comments.

3 Comments

This still has not fixed my issue.
Added my question with latest code
Sorry, yes, even with the loc, it does not work (surprised me). I updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.