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?
You can replace the missing value ( NaN ) in pandas. DataFrame and Series with any value using the fillna() method.
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.
You can try something like this :
import pandas
df = pandas.read_csv('somefile.txt')
df = df.fillna(0)
Hope that'll help !
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With