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

Viewed 33692

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?

5 Answers

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)

You can try something like this :

import pandas

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

df = df.fillna(0)

Hope that'll help !

This worked for me:

df.fillna(0, inplace=True)

Putting this into the read_csv function does work: dtype={"count": pandas.Int64Dtype()}

i.e.

df = pd.read_csv('file.csv')

This type supports both integers and pandas.NA values so you can import without the floats becoming integers.

If necessary, you can then use regular DataFrame commands to clean up the missing values, as described in other answers here.

BTW, my first attempt to solve this changes the integers into strings. If that's of interest: df = pd.read_csv('file.csv', na_filter= False)

(It reads the file without replacing any missing values with NaN).

You can replace the NaNs after reading the csv file. For example, convert the NaNs to 0:

df = pd.read_csv('file.csv')

df.fillna(0,1,inplace=True)

Using the parameter na_values, like df = pd.read_csv('file.csv', na_values='-'), has nothing to do with this.

Related