#两种方法实现
'''
1)DataFrame.reindex:调整dataframe的索引以适应新的索引
2) DataFrame.resample,可以对时间序列重新采样,支持补充缺失值
'''
import pandas as pd
import os
%matplotlib inline
df = pd.DataFrame({"pdate":['2019-12-01','2019-12-02','2019-12-04','2019-12-05'],'pv':[100,200,400,500],'uv':[10,20,40,50],
})
df
pdate | pv | uv | |
---|---|---|---|
0 | 2019-12-01 | 100 | 10 |
1 | 2019-12-02 | 200 | 20 |
2 | 2019-12-04 | 400 | 40 |
3 | 2019-12-05 | 500 | 50 |
#df缺少2019-12-03这个日期
df.set_index('pdate').plot()
<matplotlib.axes._subplots.AxesSubplot at 0x1d68fb97e10>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cScC4omN-1601386256504)(output_2_1.png)]
#方法一
pdate | pv | uv | |
---|---|---|---|
0 | 2019-12-01 | 100 | 10 |
1 | 2019-12-02 | 200 | 20 |
2 | 2019-12-04 | 400 | 40 |
3 | 2019-12-05 | 500 | 50 |
df_date = df.set_index('pdate')
df_date
#将索引设置为日期,并且将index改为时间序列的格式
pv | uv | |
---|---|---|
pdate | ||
2019-12-01 | 100 | 10 |
2019-12-02 | 200 | 20 |
2019-12-04 | 400 | 40 |
2019-12-05 | 500 | 50 |
df_date.index
Index(['2019-12-01', '2019-12-02', '2019-12-04', '2019-12-05'], dtype='object', name='pdate')
#将DataFrame的index从字符串转换成时间序列,再输出相应index为时间序列的DataFrame
df_date = df_date.set_index(pd.to_datetime(df_date.index))
df_date.index
DatetimeIndex(['2019-12-01', '2019-12-02', '2019-12-04', '2019-12-05'], dtype='datetime64[ns]', name='pdate', freq=None)
pdate = pd.date_range(start = '2019-12-01',end = '2019-12-05')
pdate
DatetimeIndex(['2019-12-01', '2019-12-02', '2019-12-03', '2019-12-04','2019-12-05'],dtype='datetime64[ns]', freq='D')
df_date_new = df_date.reindex(pdate,fill_value=0)#注意,重新设置index是reindex,不是set_index
df_date_new
pv | uv | |
---|---|---|
2019-12-01 | 100 | 10 |
2019-12-02 | 200 | 20 |
2019-12-03 | 0 | 0 |
2019-12-04 | 400 | 40 |
2019-12-05 | 500 | 50 |
‘’’
reindex()方法用于创建一个符合新索引的新对象
①对于Series类型,调用reindex()会将数据按照新的索引进行排列,如果某个索引值之前不存在,则引入缺失值
如:
②DataFrame中,reindex()可以改变行索引和列索引
reset_index(),顾名思义,即设置索引。可以设置单索引和复合索引
调用这个函数会生成一个新的DataFrame, 新的df使用一个列或多个列作为索引
reset_index(),它是set_index()的反操作,调用它分层索引的索引层级会被还原到列中
‘’’
df_date_new.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x1d691b08a58>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GkR5Se3o-1601386256511)(output_10_1.png)]
#方法二:pandas.resample方法
df
pdate | pv | uv | |
---|---|---|---|
0 | 2019-12-01 | 100 | 10 |
1 | 2019-12-02 | 200 | 20 |
2 | 2019-12-04 | 400 | 40 |
3 | 2019-12-05 | 500 | 50 |
df_new2 = df.set_index(pd.to_datetime(df['pdate']))
#
df_new2
pdate | pv | uv | |
---|---|---|---|
pdate | |||
2019-12-01 | 2019-12-01 | 100 | 10 |
2019-12-02 | 2019-12-02 | 200 | 20 |
2019-12-04 | 2019-12-04 | 400 | 40 |
2019-12-05 | 2019-12-05 | 500 | 50 |
df_new2 = df.set_index(pd.to_datetime(df['pdate'])).drop('pdate',axis=1)
df_new2
pv | uv | |
---|---|---|
pdate | ||
2019-12-01 | 100 | 10 |
2019-12-02 | 200 | 20 |
2019-12-04 | 400 | 40 |
2019-12-05 | 500 | 50 |
df_new3 = df_new2.resample('D').mean().fillna(0)
df_new3
pv | uv | |
---|---|---|
pdate | ||
2019-12-01 | 100.0 | 10.0 |
2019-12-02 | 200.0 | 20.0 |
2019-12-03 | 0.0 | 0.0 |
2019-12-04 | 400.0 | 40.0 |
2019-12-05 | 500.0 | 50.0 |
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态