今天老板要处理一批带有时间序列的数据,源数据为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. js实现鼠标单击或者双击事件

    // timer为全局变量 getClickEmail1(_type) { clearTimeout(this.timer); if (_type == 1) { if (event.detail = ...

  2. AD走圆弧走线

    美式键盘: “shift  +  空格”

  3. python3.7使用etree遇到的问题

    使用python3.6时安装好lxml时按照许多网上的教程来引入会发现etree没被引入进来 解决办法: 一.import lxml.htmletree = lxml.html.etree这样就可以使 ...

  4. 学习spring的第二天

    对昨天的查漏:关于<bean>标签的scope属性,是由它决定原型和单例的,而不是说你java代码中用到了单例模式就是单例了. 其二就是lazy-init属性,它对于scope=" ...

  5. Codeforces 1296C - Yet Another Walking Robot

    题目大意: 给定一个机器人的行走方式 你需要取走一段区间 但要保证取走这段区间后机器人最终到达的终点位置是不变的 问这段区间最短时是哪一段 解题思路: 易得,如果重复走到了某些已经走过的点,那么肯定就 ...

  6. Python String startswith() Method

    一,摘自官方API  https://docs.python.org/3/library/stdtypes.html#methods str.startswith(prefix[, start[, e ...

  7. html_js

    <!-- js的特点:别名脚本 -由浏览器内置的JavaScript引擎执行代码. -解析执行:事先不编译,逐行执行 -面向对象:内置大量的现成对象 适宜: -客户端的数据计算:不需要保存和提交 ...

  8. sendmail 的安装、配置与发送邮件的具体实现

    Ubuntu 中sendmail 的安装.配置与发送邮件的具体实现 centos安装sendmail与使用详解 CentOS下搭建Sendmail邮件服务器 使用外部SMTP发送邮件  使用mailx ...

  9. h5-任意元素居中

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. KVM以及其虚拟机安装

    一.KVM安装 安装:yum -y install kvm python-virtinst libvirt tunctl bridge-utils virt-manager qemu-kvm-tool ...