今天老板要处理一批带有时间序列的数据,源数据为1秒钟一行的csv数据,处理之后变成15分钟一行的数据。

源数据示例如下:

               time     B00    B01      ...           RollMean2.5     RollMean10
2018-05-31 09:44:39 15.212 5.071 ... 2.97 2.99
2018-05-31 09:44:40 17.202 4.047 ... 2.90 3.08
2018-05-31 09:44:41 10.137 4.055 ... 2.58 2.71
2018-05-31 09:44:42 11.961 1.994 ... 2.39 2.49
2018-05-31 09:44:43 17.157 2.019 ... 2.44 2.53
2018-05-31 09:44:44 12.972 3.991 ... 2.44 3.29
2018-05-31 09:44:45 20.078 6.023 ... 2.49 3.21

具体操作步骤如下:

(1)读取csv数据:

f = pd.read_csv(os.path.join(path1, file))

(2)将time列转换为 DatetimeIndex类型作为index值,删除time列:

f.index = pd.to_datetime(f.time.values)
del f.time

(3)使用resample函数重采样数据:

# ‘15T’表示间隔15分钟,其他间隔方式可自行查看文档说明
# sum()函数表示求和,还可以用mean()函数进行平均,其他计算方式暂时不明
# resample函数中可以通过 on=‘列名’ 关键字参数设置针对其他列名的重采样操作
resample = f.resample('15T').sum()

(4)将reample写入excel:

resample.to_excel(path1+'/'+csvf[0]+'.xlsx')

整个代码示例:

import os
import sys
import copy
import numpy as np
import pandas as pd
import openpyxl # 获取当前脚本及数据文件夹路径
path = os.path.split(sys.argv[0])[0]
# 获取当前路径下文件夹名称
dirs = [x for x in os.listdir(path) if not os.path.splitext(x)[1]]
# 遍历当前路径文件夹内文件,读取合并数据
for dir_ in dirs:
path1 = os.path.join(path, dir_)
files = copy.copy(os.listdir(path1))
for file in files:
csvf = os.path.splitext(file)
if csvf[1] == '.csv':
f = pd.read_csv(os.path.join(path1, file))
f.index = pd.to_datetime(f.time.values)
del f['time']
resample = f.resample('15T').sum()
print(csvf[0])
resample.to_excel(path1+'/'+csvf[0]+'.xlsx')

问题:excel或者csv的时间表示方式有时是以小数形式进行的,这次尚未学习如何将这种时间表示形式直接转换为DatetimeIndex类型,如果有同学知道,欢迎赐教,谢谢!

  

python pandas 对带时间序列的数据进行重采样处理的更多相关文章

  1. python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix

    先手工生出一个数据框吧 import numpy as np import pandas as pd df = pd.DataFrame(np.arange(0,60,2).reshape(10,3) ...

  2. Python利用openpyxl带格式统计数据(2)- 处理mysql数据

    上一篇些了openpyxl处理excel数据,再写一篇处理mysql数据的,还是老规矩,贴图,要处理的数据截图: 再贴最终要求的统计格式截图: 第三贴代码: 1 ''' 2 #利用openpyxl向e ...

  3. Python利用openpyxl带格式统计数据(1)- 处理excel数据

    统计数据的随笔写了两篇了,再来一篇,这是第三篇,前面第一篇是用xlwt写excel数据,第二篇是用xlwt写mysql数据.先贴要处理的数据截图: 再贴最终要求的统计格式截图: 第三贴代码: 1 '' ...

  4. python pandas.Series&&DataFrame&& set_index&reset_index

    参考CookBook :http://pandas.pydata.org/pandas-docs/stable/cookbook.html Pandas set_index&reset_ind ...

  5. python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言)

    python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言) 感觉要总结总结了,希望这次能写个系列文章分享分享心得,和大神们交流交流,提升提升. 因为 ...

  6. Python pandas检查数据中是否有NaN的几种方法

    Python pandas: check if any value is NaN in DataFrame # 查看每一列是否有NaN: df.isnull().any(axis=0) # 查看每一行 ...

  7. 基于tornado python pandas和bootstrap上传组件的mongodb数据添加工具

    总体思路:基于bootstrap4的前端页面上传组件,把excel文件上传至服务器,并利用python pandas读取里面的数据形成字典列表 通过pymongo 接口把数据插入或追加到mongodb ...

  8. Python——Pandas 时间序列数据处理

    介绍 Pandas 是非常著名的开源数据处理库,我们可以通过它完成对数据集进行快速读取.转换.过滤.分析等一系列操作.同样,Pandas 已经被证明为是非常强大的用于处理时间序列数据的工具.本节将介绍 ...

  9. oracle数据据 Python+Pandas 获取Oracle数据库并加入DataFrame

    import pandas as pd import sys import imp imp.reload(sys) from sqlalchemy import create_engine impor ...

随机推荐

  1. 把Ubuntu系统自带的源修改为国内的源,中科大源链接:https://mirrors.ustc.edu.cn/repogen/

    https://mirrors.ustc.edu.cn/repogen/ Tips: 可通过 URL 的形式直接下载配置. 例如: https://mirrors.ustc.edu.cn/repoge ...

  2. Java算法练习—— Z 字形变换

    题目链接 题目描述 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L ...

  3. zabbix监控oracle之orabbix安装

    zabbix服务器安装配置 zabbixserver中安装该软件 mkdir /opt/orabbix cp orabbix-1.2.3.zip /opt/orabbix cd /opt/orabbi ...

  4. 多线程开发之NSThrea

    创建并启动 先创建线程,再启动 // 创建   NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector( ...

  5. idea xml文件去掉背景黄色

    编写dao中的sql时,xml文件中背景一大片黄色,看着不舒服,如何去掉了? 1. File -> Settings... 2. 去消以下两项勾选    (Inspections    -- 如 ...

  6. 阿里云云服务器测试uwgis的基本流程

    基本背景 uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换. 要注意 WSGI / uwsg ...

  7. 使用maven打包问题

    项目打包:选择项目 右键->run as-> maven install . 项目中使用的是maven项目,将项目打包成war的时候有时候会出现 出现这种情况的时候解决步骤如下: 选择要打 ...

  8. 设置MySQL客户端连接使用的字符集

    设置MySQL客户端连接使用的字符集 时间:2014-03-05    来源:服务器之家    投稿:root 考虑什么是一个"连接":它是连接服务器时所作的事情.客户端发送SQL ...

  9. html+css web storage课上笔记 2019.3.18

    存储 cookie cookie 使用文本来存储信息 使用时服务器发送cookie给客户端,下一次时,浏览器发送给服务器 web storage local storage 本地的硬件设备中,关闭后不 ...

  10. mysql中各种复杂的增删改查

    1.mysql查出数据表中连续出现三次或三次以上的数据 建一张表如下:表名为 number 1.1 要求找出num列连续出现三次或三次以上的数据: select * from number where ...