绘制Dataframe列 - datetime [英] Plotting Dataframe column - datetime
本文介绍了绘制Dataframe列 - datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个日期时间列,时间相当随机,格式为:
time
-08 11:29:30
2016-07-08 11:30:02
现在我把它转换成datetime:
df ['time2'] = pd.to_datetime(df ['time'])
然后我想使用matplotlib绘制,但不起作用:
plt.plot(df。['time'],df ['y'])
pre>
我尝试将其转换为int,但是在绘制
$ b时,我无法弄明白如何格式化$ bdf ['time_int'] = df ['time2']。astype(np.int64)
任何帮助都会很棒!
解决方案我想你可以使用< a href =http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.plot.html =nofollow noreferrer>
Series.plot
,所以第一个
另一个解决方案是:
code> df ['time'] = pd.to_datetime(df.time)
df.plot(x ='time',y ='y')
plt.show()
I have a datetime column with pretty random increments of time, format is:
time 2016-07-08 11:29:30 2016-07-08 11:30:02
Now I convert it to datetime:
df['time2'] = pd.to_datetime(df['time'])
Then I want to plot it using matplotlib, but it doesn't work:
plt.plot(df.['time'],df['y'])
I've tried converting it to an int, but then I can't figure out how to format it when plotting
df['time_int'] = df['time2'].astype(np.int64)
Any help would be awesome!
解决方案I think you can use
Series.plot
, so firstset_index
from columntime
:import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({'y': {0: 1, 1: 2, 2: 4}, 'time': {0: '2016-07-08 11:29:30', 1: '2016-07-08 11:30:02', 2: '2016-07-08 11:31:52'}}) print (df) time y 0 2016-07-08 11:29:30 1 1 2016-07-08 11:30:02 2 2 2016-07-08 11:31:52 4 df['time'] = pd.to_datetime(df.time) print (df.set_index('time').y) time 2016-07-08 11:29:30 1 2016-07-08 11:30:02 2 2016-07-08 11:31:52 4 Name: y, dtype: int64 df.set_index('time').y.plot() plt.show()
Another solution is:
df['time'] = pd.to_datetime(df.time) df.plot(x='time', y='y') plt.show()
这篇关于绘制Dataframe列 - datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文