sai*_*tam 11 javascript python resampling node.js pandas
我需要在node.js. 所以我想知道是否有一个工具javascript可以像pandas在 Python 中一样工作?
假设我有与此示例类似的数据:
[{
"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
}]
Run Code Online (Sandbox Code Playgroud)
在Python我将这些数据放入pandas数据帧中,然后将其重新采样到具有不同采样率的新数据帧中。在此示例中为每日数据:
import pandas
df = pandas.DataFrame(...)
df_days = df.resample('1440min').apply({'value1':'sum', 'value2':'sum'}).fillna(0)
Run Code Online (Sandbox Code Playgroud)
所以我的新数据看起来像这样:
[{
"time": "28-09-2018 00:00:00",
"value1": 1880,
"value2": 1500
},
{
"time": "29-09-2018 00:00:00",
"value1": 1240,
"value2": 600
}]
Run Code Online (Sandbox Code Playgroud)
通常在node.js/ 中执行此操作的最佳方法是什么javascript?
简单的方法
flask应用程序,可以pandas为您进行处理JQueryAJAX就可以使用它。<!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>
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1405 次 |
| 最近记录: |