如下:
數據文件: 上海機場 (sh600009) | 24.11 | 3.58 |
東風汽車 (sh600006) | 74.25 | 1.74 |
中國國貿 (sh600007) | 26.38 | 2.66 |
包鋼股份 (sh600010) | 61.01 | 2.35 |
武鋼股份 (sh600005) | 75.85 | 1.3 |
浦發銀行 (sh600000) | 6.65 | 0.96 |
在使用read_csv() API讀取CSV文件時求取某一列數據比較大小時,
df=pd.read_csv(output_file,encoding='gb2312',names=['a','b','c'])df.b>20
報錯
TypeError:'>'not supported between instances of 'str' and 'int'
從返回的錯誤信息可知應該是數據類型錯誤,讀回來的是‘str'
in : df.dtypesout: a object b object c object dtype: object
由此可知 df.b 類型是 object
查閱read_csv()文檔 配置:
dtype : Type name or dict of column -> type, default NoneData type for data or columns. E.g. {'a': np.float64, 'b': np.int32} (unsupported with engine='python'). Use str or object to preserve and not interpret dtype.New in version 0.20.0: support for the Python parser.
可知默認使用‘str'或‘object'保存
因此在讀取時只需要修改 'dtype' 配置就可以
df=pd.read_csv(output_file,encoding='gb2312',names=['a','b','c'],dtype={'b':np.folat64})
以上這篇解決pandas使用read_csv()讀取文件遇到的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答