Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in pd.read_csv to replace NaN value with other character?

Tags:

python

pandas

csv

I have some data in csv file. Because it is collected from machine,all lines should be number but some NaN values exists in some lines.And the machine can auto replace these NaN values with a string '-'.

My question is how to set params of pd.read_csv() to auto replace '-'values with zero from csv file?

like image 599
Will Shaw Avatar asked Nov 14 '17 12:11

Will Shaw


People also ask

How do I change NaN values with different values in pandas?

You can replace the missing value ( NaN ) in pandas. DataFrame and Series with any value using the fillna() method.

How do I skip NaN in pandas?

By using dropna() method you can drop rows with NaN (Not a Number) and None values from pandas DataFrame. Note that by default it returns the copy of the DataFrame after removing rows.


2 Answers

You can try something like this :

import pandas

df = pandas.read_csv('somefile.txt')

df = df.fillna(0)

Hope that'll help !

like image 200
Saghe Achraf Avatar answered Nov 10 '22 01:11

Saghe Achraf


while reading the csv file you can use the parameter na_values:

df = pd.read_csv('file.csv',na_values='-')

Edit: you can then convert nan to 0 by:

df.fillna(0,1,inplace=True)
like image 28
shivsn Avatar answered Nov 09 '22 23:11

shivsn