I'm reading a flat file with read_csv
and flat file as some NaN values because of which it is changing the datatype to float instead of integer which I dont want.
Is there any way to replace the NaN values with 0 in the read_csv
method?
I know I can do df.fillna(value=0)
but my question is how can I replace these nan values in the read_csv
.
decop=pd.read_csv("C:\\Users\\mnk044\\Downloads\\amna_decp_part-m-00000", sep="\x01", names=clist)
Filling NaN values while loading data is currently not supported (see the read_csv
docs for information on all the supported functionality).
Your only option is to first read, and then call fillna
.
df = pd.read_csv(filepath)
df.fillna(0, inplace=True)
Next, if you want to convert your float columns to integers, then filter on df.dtypes
.
c = df.columns[df.dtypes == float]
df[c] = df[c].astype(int)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.