如何使pd.read_csv读为np.nan'null'?

时间:2019-01-07 22:17:20

标签: pandas csv

我有一个csv文件:

Date,Open,High,Low,Close,Adj Close,Volume
1980-12-12,0.513393,0.515625,0.513393,0.513393,0.023106,117258400
1980-12-15,0.488839,0.488839,0.486607,0.486607,0.021900,43971200
1980-12-16,0.453125,0.453125,0.450893,0.450893,0.020293,26432000
1980-12-17,0.462054,0.464286,0.462054,0.462054,0.020795,21610400
1980-12-18,0.475446,0.477679,0.475446,0.475446,0.021398,18362400
1980-12-19,0.504464,0.506696,0.504464,0.504464,0.022704,12157600
1980-12-22,null,null,null,null

pd.read_csv 可以将csv读入数据帧,但无法将null传输到 np.nan ,而是将所有内容读取为str。

我可以在以后的过程中手动完成这项工作,但是列类型的更改很麻烦,因为我需要将int和浮点数一一区分,有什么办法可以使用read_csv最初将null当作np.nan处理吗?

谢谢。

1 个答案:

答案 0 :(得分:2)

pd.read_csv(na_values = 'null', sep = ',')