到目前为止,我有一个看起来像这样的数据集:
2011-11-01 05:20:00 00:10:00
# z speed dir W sigW bck error
30 4.76 238.9 0.01 0.13 7.56E+06 0
40 5.24 237.1 -0.05 0.12 5.99E+06 0
50 6.33 236.6 -0.01 0.12 7.24E+06 0
60 7.06 237.3 -0.01 0.12 9.15E+06 0
70 7.85 238.2 -0.02 0.13 8.47E+06 0
80 8.85 237.3 -0.03 0.14 1.05E+07 256
2011-11-01 05:30:00 00:10:00
# z speed dir W sigW bck error
30 4.40 234.8 0.08 0.12 1.33E+07 0
40 5.07 234.2 0.11 0.12 5.82E+06 0
50 5.75 234.3 0.12 0.12 6.61E+06 0
60 6.56 232.4 0.08 0.13 6.39E+06 0
70 7.22 233.2 0.10 0.13 5.64E+06 0
80 8.15 235.3 0.12 0.14 5.87E+06 256
它会在一整天内每十分钟分成这样的带有日期时间标题的数据 block 。我想读入这些标题并将它们存储为时间变量。到目前为止,我是这样阅读的:
import pandas as pd
import glob
filename = glob.glob('1511??.mnd')
data_nov15_hereford = pd.DataFrame()
frames = []
dates = []
for i in filename:
f_nov15_hereford = pd.read_csv(i, skiprows = 32, sep='\s+')
counter = 1
for line in i:
if counter % 31 == 0:
dates.append(parse_date(line))
counter = 0
else:
counter += 1
frames.append(f_nov15_hereford)
data_nov15_hereford = pd.concat(frames,ignore_index=True)
data_nov15_hereford = data_nov15_hereford.convert_objects(convert_numeric=True)
hub_wspd = data_nov15_hereford[data_nov15_hereford['#'] == 80].z
skiprows 语句暂时跳过这些时间 header 。我有一个我感兴趣的变量 hub_wspd 以及时间 header 。有没有办法读取这些时间 header 并将它们存储为变量,而不会弄乱我已经完成的工作?谢谢!
请您参考如下方法:
如果您想将它们存储为日期时间,您可以编写一个小方法在解析标题行后返回一个 datetime 变量。
像这样
import datetime
def parse_date(string):
# Split the string into year/month/date, time, and seconds
split_string = string.split()
# get year month and date
year = split_string[0].split('-')[0]
month = split_string[0].split('-')[1]
date = split_string[0].split('-')[2]
# get hour minute second
hour = split_string[1].split(':')[0]
min = split_string[1].split(':')[1]
second = split_string[1].split(':')[2]
return datetime.datetime(year, month, date, hour=hour, minute=min, second=second)
编辑 - 完全取消计数器,只查找以“20”开头的行:
def parse_date(string):
# Split the string into year/month/date, time, and seconds
split_string = string.split()
# get year month and date
year = split_string[0].split('-')[0]
month = split_string[0].split('-')[1]
date = split_string[0].split('-')[2]
# get hour minute second
hour = split_string[1].split(':')[0]
minute = split_string[1].split(':')[1]
second = split_string[1].split(':')[2]
return datetime.datetime(int(year), int(month), int(date), hour=int(hour), minute=int(minute), second=int(second))
filename = open('./data.txt', 'r')
frames = []
dates = []
counter = 1
for line in filename:
# print(counter % 31)
if line.startswith('20'):
print(line)
dates.append(parse_date(line))
# frames.append(f_nov15_hereford)
print(dates)
