简体   繁体   English

绘制时间戳的DataFrame图

[英]Plot a graph of a DataFrame of Timestamps

I got a .csv -file containing lists of time spans like this: 我有一个包含时间跨度列表的.csv文件,如下所示:

00:00:00.000580;00:00:00.000893;00:00:00.001355;00:00:00.001767;00:00:00.001973;00:00:00.002694; 00:00:00.000580; 00:00:00.000893; 00:00:00.001355; 00:00:00.001767; 00:00:00.001973; 00:00:00.002694; 00:00:00.000069;00:00:00.000689;00:00:00.000873;00:00:00.001097;00:00:00.001920;00:00:00.002563; 00:00:00.000069; 00:00:00.000689; 00:00:00.000873; 00:00:00.001097; 00:00:00.001920; 00:00:00.002563; 00:00:00.000812;00:00:00.001307;00:00:00.001701;00:00:00.002561;00:00:00.003196;00:00:00.003600; 00:00:00.000812; 00:00:00.001307; 00:00:00.001701; 00:00:00.002561; 00:00:00.003196; 00:00:00.003600; 00:00:00.000702;00:00:00.001229;00:00:00.001750;00:00:00.002014;00:00:00.002633;00:00:00.003152; 00:00:00.000702; 00:00:00.001229; 00:00:00.001750; 00:00:00.002014; 00:00:00.002633; 00:00:00.003152; 00:00:00.000776;00:00:00.001774;00:00:00.001989;00:00:00.002115;00:00:00.002504;00:00:00.003228; 00:00:00.000776; 00:00:00.001774; 00:00:00.001989; 00:00:00.002115; 00:00:00.002504; 00:00:00.003228;

... ...

I'd like to use a jupyter notebook and pandas to read in this file and print a simple graph of the data. 我想使用jupyter笔记本和pandas来读取这个文件并打印一个简单的数据图表。 This is the code I'm using: 这是我正在使用的代码:

import pandas as pd

inputFile = "D:\\times.csv"

names = ['First', 'Second', 'Third', 'Fourth', 'Fivth', 'Sixth'] 
usecols = [0,1,2,3,4,5]
data = pd.read_csv(inputFile, usecols=usecols, sep=';', header=None, names=names, parse_dates=True)

#data.head()
data.plot.bar()

Whenever I run my notbook, I get an error message 每当我运行我的notbook时,我都会收到一条错误消息

TypeError: Empty 'DataFrame': no numeric data to plot TypeError:Empty'DataFrame':没有要绘制的数字数据

It seems the csv-file is imported correctly, since I can write out the content of my DataFrame using data.head() and it looks ok. 似乎正确导入了csv文件,因为我可以使用data.head()写出我的DataFrame的内容,看起来没问题。

What do I need to do to plot a graph from my data? 从数据中绘制图表需要做什么?

Edit: 编辑:

This is what I get from data.info() : 这是我从data.info()得到的:


RangeIndex: 200 entries, 0 to 199 RangeIndex:200个条目,0到199
Data columns (total 6 columns): 数据列(共6列):
First 200 non-null object 前200个非空对象
Second 200 non-null object 第二个200个非空对象
Third 200 non-null object 第三个200非空对象
Fourth 200 non-null object 第四个200非空对象
Fivth 200 non-null object Fivth 200非空对象
Sixth 200 non-null object 第六个200非空对象
dtypes: object(6) dtypes:对象(6)
memory usage: 9.5+ KB 内存使用量:9.5+ KB

You can convert all data to_timedelta and then to total_seconds - integers, so can be plot. 您可以将所有数据转换为to_timedelta然后转换为total_seconds - 整数,因此可以绘制。 Apply is used because to_timedelta works only with Series (columns of df ) Apply是因为to_timedelta仅适用于Seriesdf列)

data = data.apply(lambda x: pd.to_timedelta(x).dt.total_seconds())

data.plot.bar()

图形

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM