【问题标题】:How to format a pandas dataframe for use with pandas-highcharts within Django如何格式化 pandas 数据框以在 Django 中与 pandas-highcharts 一起使用
【发布时间】:2017-10-06 20:47:25
【问题描述】:

我正在尝试在 Django 项目中使用 pandas-highcharts;我能够生成图表,但没有绘制数据,所以我猜测我的 pandas 数据框格式不正确,或者我没有使用正确的方法来呈现模板。

结果:

我的数据框:

                           Entrée d'eau - Archimède - 0013A2004166CFCD
timestamp                                                             
2016-12-23 00:05:18+00:00                                         29.0
2016-12-23 00:05:27+00:00                                         29.0
2016-12-23 00:05:37+00:00                                         29.0
2016-12-23 00:05:47+00:00                                         29.0
2016-12-23 00:05:58+00:00                                         29.0

我的看法:

from django.shortcuts import render
from data.models import Value
import pandas as pd
from pandas_highcharts.core import serialize

# [...]

df = pd.DataFrame.from_records(
    Value.objects.filter(device=a).values("timestamp", "leak_value"))

df.dropna(inplace=True)  # not sure about this
df.set_index("timestamp", inplace=True)
df.sort_index(inplace=True)
df = df.truncate(
    before=pd.to_datetime(request.POST.get("start")),
    after=pd.to_datetime(request.POST.get("stop")))
df = df.rename(
    index=str,
    columns={"leak_value": "{} - {} - {}".format(
        Room.objects.filter(unit=unit_sel).get(device=a),
        Device.objects.get(address=a).devicetype,
        a)})

print(df.head())  # DEBUG

chart = serialize(
    df=df,
    render_to='Leak Values',
    title="Leak Values",
    output_type='json')

return render(request, "leak_chart.html", context={"chart": chart})

我的模板(我在 base.html 中包含 jquery 和 highcharts):

{% extends "base.html" %}

{% block body %}

    {% load staticfiles %}

    <div id="Leak Values"></div>

    <script type="text/javascript">
      new Highcharts.Chart({{chart|safe}});
    </script>

{% endblock %}

页面来源: https://pastebin.com/EkJYQPLQ

顺便说一句,我还没有找到 pandas-highcharts 的标签,而且我认为我没有创建它的权限。我正在使用 pandas-highcharts 0.5.2

编辑:这个question 似乎相关,但我无法将答案应用于我的具体情况。

【问题讨论】:

  • 你能检查一下传递给图表的leak_chart.html的格式是什么吗??它甚至是JSON格式..??
  • 系列应该是这样的:series: [{ name: 'Jane', data: [1, 0, 4] }, { name: 'John', data: [5, 7, 3] }]这是我生成图表后从页面源中提取的:"series": [{ "data": [ ["2016-12-23 00:05:18+00:00", 29.0], ["2016-12-23 00:05:27+00:00", 29.0], ["2016-12-23 19:11:34+00:00", 29.0] ], "yAxis": 0, "name": "Entr\u00e9e d'eau - Archim\u00e8de - 0013A2004166CFCD" }]

标签: python django highcharts


【解决方案1】:

Highcharts 中的类别用作轴标签的值。如果要将点的坐标(x、y 或 z)分配给类别,则应使用 categories 数组中的类别索引:

xAxis: {
  categories: ['cat1', 'cat2']
}

series: [{
  data: [
    [0 /*refers to the 'cat1' category */, someValue], 
    [1,/*refers to the 'cat1' category */, someValue]
  ]
}]

我认为在此示例中,更好的方法是使用 x 轴的 datetime 类型 (http://api.highcharts.com/highcharts/xAxis.type) 并将 x 值转换为时间戳。在这种情况下,根本不需要使用类别。

【讨论】:

  • 感谢您的回答,它引导我准确地找到如何修复我的代码以使用 pandas-highcharts。
【解决方案2】:

原来我只需要添加use_index=False,现在我的数据就显示出来了:

chart = serialize(
            df=final_df,
            render_to='Leak Values',
            title="Leak Values",
            use_index=False,
            output_type='json')

但是没有显示时间戳,所以我想我必须让 pandas-highcharts 将它们识别为日期时间,正如@Kamil Kulig 建议的那样。

【讨论】:

    猜你喜欢
    • 2015-03-05
    • 2018-11-18
    • 2021-01-25
    • 2021-01-01
    • 2019-03-14
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多