我正在从文本文件导入数据框,我想指定列的数据类型,但 Pandas 似乎忽略了dtype输入。
一个工作示例:
from io import StringIO
import pandas as pd
string = '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 int64
WBAN int64
STATION NAME object
CT object
ST float64
CALL float64
LAT float64
LON float64
ELEV(M) float64
BEGIN int64
END int64
dtype: object
为什么会发生这种情况?如何强制第一列是字符串?
相关分类