您可以使用
read_csv
函数:
import pandas as pd
import numpy as np
from pandas.compat import StringIO
import datetime as dt
temp=u"""2016 01 01 00 00 19 348 2.05 7 618.4
2016 01 01 00 01 19 351 2.05 7 618.4
2016 01 01 00 02 18 0 2.05 7 618.4
2016 01 01 00 03 17 353 2.05 7 618.4
2016 01 01 00 04 19 346 2.02 7 618.4
2016 01 01 00 05 20 345 2.00 7 618.4
2016 01 01 00 06 22 348 1.97 7 618.4"""
parser = lambda date: pd.datetime.strptime(date, '%Y %m %d %H %M')
df = pd.read_csv(StringIO(temp),
sep="\s+",
index_col=0,
date_parser=parser,
parse_dates=[[0,1,2,3,4]],
header=None)
需要设置列名,因为如果使用参数
names
,会出现以下错误:
NotImplementedError: file structure not yet supported
df.columns = ['wind_speed(kts)', 'wind_direction(dec)', 'temperature(C)', 'relative_humidity(%)', 'pressure']
df.index.name = None
print (df)
wind_speed(kts) wind_direction(dec) temperature(C) \
2016-01-01 00:00:00 19 348 2.05
2016-01-01 00:01:00 19 351 2.05
2016-01-01 00:02:00 18 0 2.05
2016-01-01 00:03:00 17 353 2.05
2016-01-01 00:04:00 19 346 2.02
2016-01-01 00:05:00 20 345 2.00
2016-01-01 00:06:00 22 348 1.97
relative_humidity(%) pressure
2016-01-01 00:00:00 7 618.4
2016-01-01 00:01:00 7 618.4
2016-01-01 00:02:00 7 618.4
2016-01-01 00:03:00 7 618.4
2016-01-01 00:04:00 7 618.4
2016-01-01 00:05:00 7 618.4
2016-01-01 00:06:00 7 618.4
print (df.dtypes)
wind_speed(kts) int64
wind_direction(dec) int64
temperature(C) float64
relative_humidity(%) int64
pressure float64
dtype: object
print (df.index)
DatetimeIndex(['2016-01-01 00:00:00', '2016-01-01 00:01:00',
'2016-01-01 00:02:00', '2016-01-01 00:03:00',
'2016-01-01 00:04:00', '2016-01-01 00:05:00',
'2016-01-01 00:06:00'],
dtype='datetime64[ns]', freq=None)