javascript - 使用 Javascript / Node.js 重新采样时间序列或数据帧
问题描述
我需要在node.js
. 所以我想知道是否有一个与Pythonjavascript
类似的工具?pandas
假设我有类似于此示例的数据:
[{
"time": "28-09-2018 21:29:04",
"value1": 1280,
"value2": 800
},
{
"time": "28-09-2018 21:38:56",
"value1": 600,
"value2": 700
},
{
"time": "29-09-2018 10:40:00",
"value1": 1100,
"value2": 300
},
{
"time": "29-09-2018 23:50:48",
"value1": 140,
"value2": 300
}]
在Python
我会将这些数据放入pandas
数据帧中,然后将其重新采样到具有不同采样率的新数据帧中。在此示例中为每日数据:
import pandas
df = pandas.DataFrame(...)
df_days = df.resample('1440min').apply({'value1':'sum', 'value2':'sum'}).fillna(0)
所以我的新数据看起来像这样:
[{
"time": "28-09-2018 00:00:00",
"value1": 1880,
"value2": 1500
},
{
"time": "29-09-2018 00:00:00",
"value1": 1240,
"value2": 600
}]
通常在node.js
/中执行此操作的最佳方法是什么javascript
?
解决方案
简单的方法
- 可以为您处理的非常简单的
flask
应用程序pandas
- 简单的
JQuery
AJAX 使用它。
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
</head>
<body>
<main id="main">
<section id="data-section">
<h2>Data</h2>
<div id="data"/>
</section>
</main>
</body>
<script>
function apicall(url, data) {
$.ajax({
type:"POST", url:url, data:{data:JSON.stringify(data)},
success: (data) => { $("#data").text(JSON.stringify(data)); }
});
}
data = [{"time": "28-09-2018 21:29:04","value1": 1280,"value2": 800},{"time": "28-09-2018 21:38:56","value1": 600,"value2": 700},{"time": "29-09-2018 10:40:00","value1": 1100,"value2": 300},
{"time": "29-09-2018 23:50:48","value1": 140,"value2": 300}];
window.onload = function () {
apicall("/handle_data", data);
}
</script>
</html>
烧瓶应用
import pandas as pd, json
from flask import Flask, redirect, url_for, request, render_template, Response
app = Flask(__name__)
@app.route('/')
@app.route('/home')
def home():
return render_template('home.html')
@app.route('/handle_data', methods=["POST"])
def handle_data():
df = pd.DataFrame(json.loads(request.form.get("data")))
df["time"] = pd.to_datetime(df["time"])
df.set_index("time", inplace=True)
df = df.resample('1440min').apply({'value1':'sum', 'value2':'sum'}).fillna(0)
return Response(json.dumps(df.to_dict(orient="records")),
mimetype="text/json")
if __name__ == '__main__':
app.run(debug=True, port=3000)
输出
推荐阅读
- javascript - Apex 图表自定义工具提示 - 如何获取类别名称和颜色?
- matlab - 手动傅里叶变换以使用高斯 PSF 模糊图像,给出反转结果
- sql-server - SQL Server 2014 的问题 - SQL Server 数据库上的锁定
- typescript - TypeScript:正确的类型,用于传递带有 reduce 的对象数组
- css - 如何确保我的 Wordpress 插件的样式不会被全局/主题样式覆盖?
- android - 如何将数据从 FCM 的 onMessageReceived() 发送到 Fragment?
- java - Angular、JSON 地图数据不会转换为地图
- javascript - Javascript 中的范围
- java - 编写包含多个具有公共字段的枚举的类的最佳方法
- r - 用for循环创建矩阵