Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

csv - How do I get pandas.read_csv to interpret a null entry as integer 0?

With pandas.read_csv, I am reading a CSV which includes null entries, e.g.,

col1,col2,col3
2,3,4
8,,9

read_csv interprets the null entry as a float nan (since there is no int nan), so the column is being read as float64. If I try to force the column to be read as an integer, I get an error:

pd.read_csv(FILE, dtype = {'col2': int})
=> ValueError: Integer column has NA values in column 1

Should I read the data as float or string and convert afterwards? Or is there some way to specify that a null entry in an int column should be interpreted as zero?

Also, is there some way to turn on more detailed error reporting? I am new to pandas and it would have been helpful to know that the error was in row 1 (obviously my real data was much bigger).

question from:https://stackoverflow.com/questions/65865838/how-do-i-get-pandas-read-csv-to-interpret-a-null-entry-as-integer-0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...