gpt4 book ai didi

pandas:read_csv 将日期时间列组合为数据帧的索引

转载 作者:行者123 更新时间:2023-12-04 05:18:16 26 4
gpt4 key购买 nike

我有一个 csv 文件,其中包含日期和时间戳作为两列。我正在使用 Pandas 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组合在 DataFrame 中。如果我尝试使用 date_time 会出现问题来自 df作为索引。指定 index = df.date_time给所有 NaN值(value)观:
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 s。如何获得中间值 f 中的正确值框架?

最佳答案

您要使用 set_index :

df1 = df.set_index('date_time')

其中选择列 'date_time'作为新 DataFrame 的索引。

.

注意:您在 DataFrame 构造函数中遇到的行为如下所示:
df = pd.DataFrame([[1,2],[3,4]])
df1 = pd.DataFrame(df, index=[1,2])

In [3]: df1
Out[3]:
0 1
1 3 4
2 NaN NaN

关于pandas:read_csv 将日期时间列组合为数据帧的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13977719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com