简体   繁体   English

如何在Django中格式化pandas数据框以与pandas-highcharts一起使用

[英]How to format a pandas dataframe for use with pandas-highcharts within Django

I'm trying to use pandas-highcharts within a Django project; 我正在尝试在Django项目中使用pandas-highcharts; I'm able to produce the graph, but the data is not plotted, so I'm guessing my pandas dataframe is not formatted correctly, or maybe I'm not using the right method to render the template. 我能够生成图形,但未绘制数据,因此我猜测我的pandas数据框格式不正确,或者我未使用正确的方法来呈现模板。

The result: 结果: 结果高图图

My dataframe: 我的数据框:

                           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

My view: 我的观点:

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})

My template (I include jquery and highcharts in base.html): 我的模板(我在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 %}

The page source: https://pastebin.com/EkJYQPLQ 页面来源: https//pastebin.com/EkJYQPLQ

By the way I haven't found a tag for pandas-highcharts and I don't think I have the privileges to create it. 顺便说一下,我还没有找到pandas-highcharts的标签,而且我认为我没有创建它的特权。 I'm using pandas-highcharts 0.5.2 我正在使用pandas-highcharts 0.5.2

EDIT: This question seems related but I'm unable to apply the answer to my specific situation. 编辑:这个问题似乎相关,但我无法将答案应用于我的具体情况。

Categories in Highcharts work as the values for axis' labels. Highcharts中的类别用作轴标签的值。 If you want to assign a point's coordinate (x, y or z) to a category you should use a category index from categories array: 如果你想指定点的坐标(X,Y或Z)的类别,则应该使用一个类指数categories排列:

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

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

I think that in this example the better approach is to use datetime type of the x axis ( http://api.highcharts.com/highcharts/xAxis.type ) and convert your x values to timestamps. 我认为在此示例中,更好的方法是使用x轴的datetime类型( http://api.highcharts.com/highcharts/xAxis.type )并将x值转换为时间戳。 In this case there's no need to use categories at all. 在这种情况下,根本不需要使用类别。

Turns out all I had to change was to add use_index=False , now my data is shown: 原来我use_index=False就是添加use_index=False ,现在显示了我的数据:

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

However the timestamps are not shown, so I guess I'll have to make pandas-highcharts recognize them as datetime, as @Kamil Kulig suggested. 但是没有显示时间戳,所以我想必须像@Kamil Kulig所建议的那样,使大熊猫图表将其识别为日期时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM