我有一个csv文件,其中包含日期和时间戳记作为两列。我使用熊猫read_csv
将内容读入数据框。我的最终目标是从数据中绘制时间序列图。熊猫:read_csv组合日期时间列作为索引到数据框中
!head vmstat.csv
wait_proc,sleep_proc,swapped_memory,free_memory,buffered_memory,cached_memory,swapped_in,swapped_out,received_block,sent_block,interrups,context_switches,user_time,sys_time,idle_time,wait_io_time,stolen_time,date,time
0,0,10896,3776872,380028,10284052,0,0,6,16,7716,4755,3,1,96,0,0,2012-11-01,08:59:27
0,0,10896,3776500,380028,10284208,0,0,0,40,7471,4620,0,0,99,0,0,2012-11-01,08:59:32
0,0,10896,3749840,380028,10286864,0,0,339,19,7479,4704,20,2,77,1,0,2012-11-01,08:59:37
0,0,10896,3747536,380028,10286964,0,0,17,118,7488,4638,0,0,99,0,0,2012-11-01,08:59:42
0,0,10896,3747452,380028,10287148,0,0,0,24,7489,4676,0,0,99,0,0,2012-11-01,08:59:47
df = read_csv("vmstat.csv", parse_dates=[['date','time']])
f = DataFrame(df, columns=[ 'date_time', 'user_time', 'sys_time', 'wait_io_time'])
In [3]: f
Out[3]:
date_time user_time sys_time wait_io_time
0 2012-11-01 08:59:27 3 1 0
1 2012-11-01 08:59:32 0 0 0
2 2012-11-01 08:59:37 20 2 1
3 2012-11-01 08:59:42 0 0 0
4 2012-11-01 08:59:47 0 0 0
到目前为止,我们可以正确地读取数据,并date_time
在数据帧相结合。如果我尝试使用df
的date_time
作为索引,则会出现问题。指定index = df.date_time
给所有NaN
值:
dindex = f['date_time']
print dindex
g = DataFrame(f, columns=[ 'user_time', 'sys_time', 'wait_io_time'], index=dindex)
In [7]: g
Out[7]:
0 2012-11-01 08:59:27
1 2012-11-01 08:59:32
2 2012-11-01 08:59:37
3 2012-11-01 08:59:42
4 2012-11-01 08:59:47
Name: date_time <---- dindex
g:
user_time sys_time wait_io_time
date_time
2012-11-01 08:59:27 NaN NaN NaN
2012-11-01 08:59:32 NaN NaN NaN
2012-11-01 08:59:37 NaN NaN NaN
2012-11-01 08:59:42 NaN NaN NaN
2012-11-01 08:59:47 NaN NaN NaN
正如你看到的,列值出来,因为所有NaN
秒。如何在中间f
框架中获得正确的值?
伟大的,谢谢!现在我得到正确的格式。我仍然需要调试为什么df1.plot失败,“对象没有属性”序号“”错误。 –
这可能应该是一个单独的问题,它应该与时间序列作为索引绘制df没有问题...:s –
谢谢,我正在做一些初始尝试,通过安装更新版本的matplotlib。如果全部失败,将提交新的qn。 –