How do i replace nan with na in python?

I have a Pandas Dataframe as shown below:

    1    2       3
 0  a  NaN    read
 1  b    l  unread
 2  c  NaN    read

I want to remove the NaN values with an empty string so that it looks like so:

    1    2       3
 0  a   ""    read
 1  b    l  unread
 2  c   ""    read

How do i replace nan with na in python?

asked Nov 10, 2014 at 6:29

user1452759user1452759

8,03613 gold badges40 silver badges54 bronze badges

0

df = df.fillna('')

This will fill na's (e.g. NaN's) with ''.

inplace is possible but should be avoided as it will be deprecated:

df.fillna('', inplace=True)

To fill only a single column:

df.column1 = df.column1.fillna('')

One can use df['column1'] instead of df.column1.

answered Feb 8, 2015 at 5:44

fantabolousfantabolous

19.6k6 gold badges52 silver badges46 bronze badges

4

import numpy as np
df1 = df.replace(np.nan, '', regex=True)

This might help. It will replace all NaNs with an empty string.

Ninjakannon

3,6516 gold badges51 silver badges72 bronze badges

answered Nov 10, 2014 at 6:40

8

If you are reading the dataframe from a file (say CSV or Excel) then use :

df.read_csv(path , na_filter=False)
df.read_excel(path , na_filter=False)

This will automatically consider the empty fields as empty strings ''


If you already have the dataframe

df = df.replace(np.nan, '', regex=True)
df = df.fillna('')

answered Jul 19, 2017 at 15:16

Natesh bhatNatesh bhat

10.9k10 gold badges72 silver badges115 bronze badges

4

Use a formatter, if you only want to format it so that it renders nicely when printed. Just use the df.to_string(... formatters to define custom string-formatting, without needlessly modifying your DataFrame or wasting memory:

df = pd.DataFrame({
    'A': ['a', 'b', 'c'],
    'B': [np.nan, 1, np.nan],
    'C': ['read', 'unread', 'read']})
print df.to_string(
    formatters={'B': lambda x: '' if pd.isnull(x) else '{:.0f}'.format(x)})

To get:

   A B       C
0  a      read
1  b 1  unread
2  c      read

smci

30.4k18 gold badges110 silver badges144 bronze badges

answered Jun 21, 2018 at 22:41

Steve SchulistSteve Schulist

8511 gold badge10 silver badges16 bronze badges

3

Try this,

add inplace=True

import numpy as np
df.replace(np.NaN, '', inplace=True)

answered Aug 23, 2019 at 12:27

How do i replace nan with na in python?

Vineesh TPVineesh TP

7,45911 gold badges60 silver badges122 bronze badges

1

using keep_default_na=False should help you:

df = pd.read_csv(filename, keep_default_na=False)

answered Jun 28, 2019 at 9:29

How do i replace nan with na in python?

If you are converting DataFrame to JSON, NaN will give error so best solution is in this use case is to replace NaN with None.
Here is how:

df1 = df.where((pd.notnull(df)), None)

taras

6,20210 gold badges37 silver badges48 bronze badges

answered Mar 15, 2018 at 20:48

How do i replace nan with na in python?

I tried with one column of string values with nan.

To remove the nan and fill the empty string:

df.columnname.replace(np.nan,'',regex = True)

To remove the nan and fill some values:

df.columnname.replace(np.nan,'value',regex = True)

I tried df.iloc also. but it needs the index of the column. so you need to look into the table again. simply the above method reduced one step.

answered Jul 4, 2019 at 4:07

How do i replace nan with na in python?

Subbu VidyaSekarSubbu VidyaSekar

2,3452 gold badges19 silver badges36 bronze badges

Not the answer you're looking for? Browse other questions tagged python pandas dataframe nan or ask your own question.

How do I change NaN to NA in Python?

Methods to replace NaN values with zeros in Pandas DataFrame:.
fillna() The fillna() function is used to fill NA/NaN values using the specified method..
replace() The dataframe. replace() function in Pandas can be defined as a simple method used to replace a string, regex, list, dictionary etc. in a DataFrame..

How do I change NaN to none?

Use pandas. DataFrmae. fillna() to Replace NaN/Null values with an empty string. This replaces each NaN in pandas DataFrame with an empty string.

What can I replace NaN with?

Replace NaN Values with Zeros in Pandas DataFrame.
(1) For a single column using Pandas: df['DataFrame Column'] = df['DataFrame Column'].fillna(0).
(2) For a single column using NumPy: df['DataFrame Column'] = df['DataFrame Column'].replace(np.nan, 0).
(3) For an entire DataFrame using Pandas: df.fillna(0).

How do I fix NaN in Python?

We can replace NaN values with 0 to get rid of NaN values. This is done by using fillna() function. This function will check the NaN values in the dataframe columns and fill the given value.