Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...