我正在从文本文件导入数据框,我想指定列的数据类型,但 Pandas 似乎忽略了dtype输入。一个工作示例:from io import StringIOimport pandas as pdstring = 'USAF WBAN STATION NAME CTRY ST CALL LAT LON ELEV(M) BEGIN END\n007026 99999 WXPOD 7026 AF +00.000 +000.000 +7026.0 20120713 20170822\n007070 99999 WXPOD 7070 AF +00.000 +000.000 +7070.0 20140923 20150926'f = StringIO(string)df = pd.read_fwf(f, colspecs = [(0,6), (7,12), (13,41), (43,45), (48,50), (51,55), (57,64), (65,73), (74,81), (82,90), (91,101)], dtypes = {'USAF' : str, 'WBAN' : str, 'STATION NAME' : str, 'CT' : str, 'ST' : str, 'CALL' : str, 'LAT' : float, 'LON' : float, 'ELEV(M)' : float, 'BEGIN' : int, 'END' : int,}, )df.dtype返回USAF int64WBAN int64STATION NAME objectCT objectST float64CALL float64LAT float64LON float64ELEV(M) float64BEGIN int64END int64dtype: object为什么会发生这种情况?如何强制第一列是字符串?
添加回答
举报
0/150
提交
取消