df = df.replace({np.nan: None})
Note: For pandas versions <1.4, this changes the dtype of all affected columns to object.
To avoid that, use this syntax instead:
df = df.replace(np.nan, None)
Note 2: If you don't want to import numpy, np.nan can be replaced with native float('nan'):
df = df.replace({float('nan'): None})
Credit goes to this guy here on this Github issue, Killian Huyghe's comment and Matt's answer.
Answer from EliadL on Stack Overflowdf = df.replace({np.nan: None})
Note: For pandas versions <1.4, this changes the dtype of all affected columns to object.
To avoid that, use this syntax instead:
df = df.replace(np.nan, None)
Note 2: If you don't want to import numpy, np.nan can be replaced with native float('nan'):
df = df.replace({float('nan'): None})
Credit goes to this guy here on this Github issue, Killian Huyghe's comment and Matt's answer.
For pandas > 1.3.0 see this answer.
@bogatron has it right, you can use where, it's worth noting that you can do this natively in pandas:
df1 = df.where(pd.notnull(df), None)
Note: this changes the dtype of all columns to object.
Example:
In [1]: df = pd.DataFrame([1, np.nan])
In [2]: df
Out[2]:
0
0 1
1 NaN
In [3]: df1 = df.where(pd.notnull(df), None)
In [4]: df1
Out[4]:
0
0 1
1 None
Note: what you cannot do recast the DataFrames dtype to allow all datatypes types, using astype, and then the DataFrame fillna method:
df1 = df.astype(object).replace(np.nan, 'None')
Unfortunately neither this, nor using replace, works with None see this (closed) issue.
As an aside, it's worth noting that for most use cases you don't need to replace NaN with None, see this question about the difference between NaN and None in pandas.
However, in this specific case it seems you do (at least at the time of this answer).
I need to replace NaN in one column with value for other col
BUG: Replacing NaN with None in Pandas 1.3 does not work
BUG: dataframe.replace({np.nan: None}) failed when replaced even number of times
Replace None with NaN in pandas dataframe - Stack Overflow
I've been working on learning Python and for something to code, I picked some VBA that I had.
In VBA:
If Cells(I, "C").Value <> "" And Cells(I, "B").Value = "" Then
Cells(I, "B").Value = Cells(I, "C").Value
End IfIt simply checks if colC is not Null and colB is Null, then replaces colB with the value from colC.
I can read in the csv file, I was able to select and delete some rows I didn't want, but I can't seem to get the syntax right for this...
You can use DataFrame.fillna or Series.fillna which will replace the Python object None, not the string 'None'.
import pandas as pd
import numpy as np
For dataframe:
df = df.fillna(value=np.nan)
For column or series:
df.mycol.fillna(value=np.nan, inplace=True)
Here's another option:
df.replace(to_replace=[None], value=np.nan, inplace=True)
Pretty straightforward, I have a dataframe that has columns with different mixtures of np.nan and None as the "null" value for that column. I want to get them all to be "None", but
df.fillna(None) df.replace(np.nan, None) df.fillna(values=None)
all don't work. Anyone run into this issue before?
Also why the bloody fucking hell does
np.nan==np.nan
evaluate to false? (Yes I know its by definition, but some bloke thought it was a good idea to write the definition that way.) Is that actually useful for anything? In this particular case, it makes me want to use None instead.