Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling list nan values

Tags:

python

pandas

How to fill nan values with 0's in a list. I can do it for dataframes but don't know how to do it for lists?

listname=listname.fillna(0)

This isn't working.

like image 257
Shirohige Avatar asked Jun 07 '17 03:06

Shirohige


1 Answers

You can convert to a pandas series and back to a list

pd.Series(listname).fillna(0).tolist()

Consider the list listname

listname = [1, np.nan, 2, None, 3]

Then

pd.Series(listname, dtype=object).fillna(0).tolist()

[1, 0, 2, 0, 3]
like image 79
piRSquared Avatar answered Sep 17 '22 21:09

piRSquared