来自Django聚合查询的熊猫
如何将聚合Q转换为数据帧来自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 谢谢。
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)
并粘贴输出吗。