Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自Django聚合查询的熊猫_Django_Pandas - Fatal编程技术网

来自Django聚合查询的熊猫

来自Django聚合查询的熊猫,django,pandas,Django,Pandas,如何将聚合Q转换为数据帧 for i in a: qs = Cashflow.objects.filter ( acct = i.id ).aggregate(Min('month'), Max('month'), Avg('value'), Count('month')) df = pd.DataFrame.from_records ( qs ) 我得到这个错误: If using all scalar values, you must pass an index 谢谢。

如何将聚合Q转换为数据帧

for i in a:
    qs = Cashflow.objects.filter ( acct = i.id ).aggregate(Min('month'), Max('month'), Avg('value'), Count('month'))

    df = pd.DataFrame.from_records ( qs )
我得到这个错误:

If using all scalar values, you must pass an index

谢谢。

我用这个来做我需要的事

for i in accounts:

    qs = Cashflow.objects.filter ( id = i.id ).aggregate(Min('month'), Max('month'), Avg('value'), Count('month'))

    temp = pd.DataFrame.from_dict ( [qs] )

    temp [ 'id' ] = i.id
    temp = temp.set_index ( 'id' )

    df = df.append ( temp )

df = df.reset_index()
结果:

     id  month__count  month__max  month__min     value__avg
0    11            34  2016-04-30  2013-07-31         326329
1    32            19  2016-04-30  2014-10-31         258317
也许有更好的办法

我希望这能有所帮助。

您能在
qs=…
后面的行中添加一个
print(qs)
并粘贴输出吗。