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

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)

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).


This worked for me:

df.fillna(0, inplace=True)

You can try something like this :

import pandas

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

df = df.fillna(0)

Hope that'll help !

Tags:

Python

Pandas

Csv