replace the NaN value zero after an operation with arrays
If you have Python 2.6 you have the math.isnan()
function to find NaN
values.
With this we can use a list comprehension to replace the NaN
values in a list as follows:
import math
mylist = [0 if math.isnan(x) else x for x in mylist]
If you have Python 2.5 we can use the NaN != NaN
trick from this question so you do this:
mylist = [0 if x != x else x for x in mylist]
Function to replace all NaN values with zero:
Use boolean mask.
Suppose the following dataframe:
>>> df
A B C
0 0.0 1 2.0
1 NaN 4 5.0 # <- NaN should be replace by 0.1
2 6.0 7 NaN # <- NaN should be replace by 0
m1 = df.isna().any() # Is there a NaN in columns (not mandatory)
m2 = df.eq(0).any() # Is there a 0 in columns
# Replace by 0
df.update(df.loc[:, m1 & ~m2].fillna(0))
# Replace by 0.1
df.update(df.loc[:, m1 & m2].fillna(0.1))
Only the second mask is useful
Output result:
>>> df
A B C
0 0.0 1 2.0
1 0.1 4 5.0
2 6.0 7 0.0
convert nan value to zero
This should work:
from numpy import *
a = array([[1, 2, 3], [0, 3, NaN]])
where_are_NaNs = isnan(a)
a[where_are_NaNs] = 0
In the above case where_are_NaNs is:
In [12]: where_are_NaNs
Out[12]:
array([[False, False, False],
[False, False, True]], dtype=bool)
Replacing NaN/Inf/NA to 0 in a list object
We can replace
the non finite values to 0 by looping over the list
elements with lapply
lapply(dat, function(x) replace(x, !is.finite(x), 0))
# [,1] [,2]
#[1,] 2 0
#[2,] 0 0
#$b
# [,1] [,2]
#[1,] -3 0
#[2,] 0 1
I want to replace NaN values with 0 but not able to with the below code
In your code you passed to_replace="NaN"
.
Note that you actually passed here a string containing just these 3 letters.
In Pandas you can pass np.nan
, but only as the value to be assigned
to a cell in a DataFrame. The same pertains to a Numpy array.
You can not pass to_replace=np.nan
, because the comparison rules are
that one np.nan is NOT equal to another np.nan.
One of possible solutions is to run:
df2 = df2.where(~df2.isna(), 0)
Other, simpler solution, as richardec suggested, is to use fillna,
but the argument should be 0 (zero) not "o" (a char):
df2 = df2.fillna(0)
Replacing nan values in a Pandas data frame with lists
You have to handle the three cases (empty string, NaN, NaN in list) separately.
For the NaN in list you need to loop over each occurrence and replace the elements one by one.
NB. applymap
is slow, so if you know in advance the columns to use you can subset them
For the empty string, replace them to NaN, then fillna
.
sub = 'X'
(df.applymap(lambda x: [sub if (pd.isna(e) or e=='')
else e for e in x]
if isinstance(x, list) else x)
.replace('', float('nan'))
.fillna(sub)
)
Output:
col1 col2 col3 col4
0 X Jhon [X, 1, 2] [k, j]
1 1.0 X [1, 1, 5] 3
2 2.0 X X X
3 3.0 Samy [1, 1, X] [b, X]
Used input:
from numpy import nan
df = pd.DataFrame({'col1': {0: nan, 1: 1.0, 2: 2.0, 3: 3.0},
'col2': {0: 'Jhon', 1: nan, 2: '', 3: 'Samy'},
'col3': {0: [nan, 1, 2], 1: [1, 1, 5], 2: nan, 3: [1, 1, nan]},
'col4': {0: ['k', 'j'], 1: '3', 2: nan, 3: ['b', '']}})
Replacing NaN with 0 in Python
This should work:
import math
[0 if math.isnan(i) else i for i in ls]
Related Topics
How to Efficiently Read the First Character from Each Line of a Text File
Group_By() into Fill() Not Working as Expected
Calculate Summary Statistics (E.G. Mean) on All Numeric Columns Using Data.Table
Select Columns by Class (E.G. Numeric) from a Data.Table
How to Build a Crossword-Like Plot for a Boolean Matrix
Getting the Error "Level Sets of Factors Are Different" When Running a for Loop
Rgdal Installation Difficulty on Ubuntu 16.04 Lts
To Display Two Heatmaps in Same PDF Side by Side in R
How to Start Ggplot2 Geom_Bar from Different Origin
Change a Column from Birth Date to Age in R
R Output Without [1], How to Nicely Format
How to Extract Multiples of a Number from a Vector
Multi Line Title in Ggplot 2 with Multiple Italicized Words
Changing Multiple Column Values Given a Condition in Dplyr
How to Calculate the Distance Between Latitude and Longitude Along Rows of Columns in R
Preventing Column-Class Inference in Fread()
Axis Labels for Each Bar and Each Group in Bar Charts with Dodged Groups