时间序列图Python [英] Time Series Plot Python

查看:369
本文介绍了时间序列图Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用熊猫,我想绘制一个时序图.我有这个数据框,我想在x轴上绘制日期,在y轴上绘制单位数.我假设我需要先将日期对象转换为日期时间,然后才能进行此绘图.

I am using pandas and I want to make a time series plot. I have this dataframe, and I want to plot the date on the x-axis with the number of units on the y-axis. I am assuming I need to convert my date object to a datetime before I can make this plot.

df1_99.dtypes

 date            object
 store_nbr        int64
 units            int64
 tavg             int64
 preciptotal    float64
 dtype: object


df1_99
        date        store_nbr      units        tavg    preciptotal
 101885 2014-10-13       1          2            49       0.00
 101996 2014-10-14       1          1            67       0.00
 102107 2014-10-15       1          0            70       0.00
 102218 2014-10-16       1          0            67       0.87
 102329 2014-10-17       1          3            65       0.01

推荐答案

由于日期是字符串,因此可以使用

As your dates are strings you can use to_datetime to convert to datetime objects:

In [4]:

df['date'] = pd.to_datetime(df['date'])
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 101885 to 102329
Data columns (total 5 columns):
date           5 non-null datetime64[ns]
store_nbr      5 non-null int64
units          5 non-null int64
tavg           5 non-null int64
preciptotal    5 non-null float64
dtypes: datetime64[ns](1), float64(1), int64(3)
memory usage: 240.0 bytes

然后您可以绘制此图:

df.plot(x='date', y='units')

这篇关于时间序列图Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆