python pandas 对带时间序列的数据进行重采样处理
今天老板要处理一批带有时间序列的数据,源数据为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 对带时间序列的数据进行重采样处理的更多相关文章
- 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) ...
- Python利用openpyxl带格式统计数据(2)- 处理mysql数据
上一篇些了openpyxl处理excel数据,再写一篇处理mysql数据的,还是老规矩,贴图,要处理的数据截图: 再贴最终要求的统计格式截图: 第三贴代码: 1 ''' 2 #利用openpyxl向e ...
- Python利用openpyxl带格式统计数据(1)- 处理excel数据
统计数据的随笔写了两篇了,再来一篇,这是第三篇,前面第一篇是用xlwt写excel数据,第二篇是用xlwt写mysql数据.先贴要处理的数据截图: 再贴最终要求的统计格式截图: 第三贴代码: 1 '' ...
- python pandas.Series&&DataFrame&& set_index&reset_index
参考CookBook :http://pandas.pydata.org/pandas-docs/stable/cookbook.html Pandas set_index&reset_ind ...
- python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言)
python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言) 感觉要总结总结了,希望这次能写个系列文章分享分享心得,和大神们交流交流,提升提升. 因为 ...
- Python pandas检查数据中是否有NaN的几种方法
Python pandas: check if any value is NaN in DataFrame # 查看每一列是否有NaN: df.isnull().any(axis=0) # 查看每一行 ...
- 基于tornado python pandas和bootstrap上传组件的mongodb数据添加工具
总体思路:基于bootstrap4的前端页面上传组件,把excel文件上传至服务器,并利用python pandas读取里面的数据形成字典列表 通过pymongo 接口把数据插入或追加到mongodb ...
- Python——Pandas 时间序列数据处理
介绍 Pandas 是非常著名的开源数据处理库,我们可以通过它完成对数据集进行快速读取.转换.过滤.分析等一系列操作.同样,Pandas 已经被证明为是非常强大的用于处理时间序列数据的工具.本节将介绍 ...
- oracle数据据 Python+Pandas 获取Oracle数据库并加入DataFrame
import pandas as pd import sys import imp imp.reload(sys) from sqlalchemy import create_engine impor ...
随机推荐
- 【转载】webDriver拾级而上·之五 iframe的处理
有时候我们在定位一个页面元素的时候发现一直定位不了,反复检查自己写的定位器没有任何问题,代码也没有任何问题.这时你就要看一下这个页面元素是否在一个iframe中,这可能就是找不到的原因之一. 如果你在 ...
- POJ 2996:Help Me with the Game
Help Me with the Game Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64 ...
- python脚本文件引用
二.Python __init__.py 作用详解 https://www.cnblogs.com/Lands-ljk/p/5880483.html __init__.py 文件的作用是将文件夹变为一 ...
- 12 —— node 获取文件属性 —— 利用 自调用 闭包函数 解决 i 丢失的问题
闭包的作用 : 保存变量 一,i 丢失的案例 var arr = ['node','vue','mysql'] for(var i=0;i<arr.length;i++){ setTimeout ...
- Arduino - Nano针脚分配时需要注意的事项
0.1为Rx.Tx 针脚,这两个针脚一般作为串口使用,非串口设备尽量不占用该针脚.2.3为中断口,分别对应中断0.中断1,需要中断功能的设备,必须接入此.2-13.A0-A5,共18个针脚,都可以作为 ...
- zabbix_server
http://www.linuxidc.com/Linux/2014-11/109909.htm [root@localhost zabbix]# service iptables stop 关闭i ...
- JS照片轮换
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- swift中实现cell中局部播放的动画效果
在cell中 // 播放器动画效果 private var replicatorLayer:ReplicatorLayer = { let layer = ReplicatorLayer.init(f ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习: Break 和 Continue 语句
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- PHP实现简易微信红包算法
<?php /** * PHP实现简易的微信红包算法 * @version v1.0 * @author quetiezheng */ function getMoney($total, $pe ...