教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 如何用datetime去除重复python3时间?

如何用datetime去除重复python3时间?

发布时间:2020-12-05   编辑:jiaochengji.com
教程集为您提供如何用datetime去除重复python3时间?等资源,欢迎您收藏本站,我们将为您提供最新的如何用datetime去除重复python3时间?资源

当我们成功运行出一段数据时,不一定里面的内容都是我们所需要的,其中代码有重复的地方必须进行筛除。当需要筛除的数据变成了时间时,我们似乎又可以选择其他的方法进行去重。就比如datetime完全可以做到这一点,相信小伙伴们比较惊讶,我们往下看:


主要分为以下两点

1).index.is_unique检查索引日期是否是唯一的

2)对非唯一时间戳的数据进行聚合,通过groupby,并传入level = 0(索引的唯一一层)

dates = pd.DatetimeIndex(['2017/06/01','2017/06/02','2017/06/02','2017/06/02','2017/06/03'])
dates
 DatetimeIndex(['2017-06-01', '2017-06-02', '2017-06-02', '2017-06-02',
   '2017-06-03'],
   dtype='datetime64[ns]', freq=None)
 
dup_ts = pd.Series(np.arange(5),index = dates)
dup_ts
 2017-06-01 0
 2017-06-02 1
 2017-06-02 2
 2017-06-02 3
 2017-06-03 4
 dtype: int32
 
dup_ts.index.is_unique
 False
dup_ts['2017-06-02']
 2017-06-02 1
 2017-06-02 2
 2017-06-02 3
 dtype: int32
 
grouped = dup_ts.groupby(level=0).mean()
grouped
 2017-06-01 0
 2017-06-02 2
 2017-06-03 4
 dtype: int32
 
dup_df = pd.DataFrame(np.arange(10).reshape((5,2)),index = dates )
dup_df
0   1
2017-06-01  0   1
2017-06-02  2   3
2017-06-02  4   5
2017-06-02  6   7
2017-06-03  8   9
grouped_df = dup_df.groupby(level=0).mean()##针对DataFrame
grouped_df


在筛选重复的时间方面,datetime同样可以做到,这可能是很多小伙伴没有想到的结果,是不是意外的收获呢~更多Python学习推荐:JQ教程网Python大全

您可能感兴趣的文章:
PHP 5.2日期、时间和时区处理详解
2019年python学3还是2
如何用datetime去除重复python3时间?
mysql存储过程中常用的基本函数
datetime怎样在python3时间中执行循环?
python中如何比较两个时间点
Python3爬虫实战:Appium 爬取微信朋友圈
分享:mysql存储过程的基本函数
时间戳在Python3中转换为指定格式日期的方法
Python3时间戳如何应用于数学计算?

[关闭]
~ ~