繁体   English   中英

使用数据透视表时,没有数值类型会聚合错误

[英]Getting No numeric types to aggregate error when using pivot_table

我知道这确实很容易,但是在尝试进行透视操作时会不断出错。 我从psycopg2获取数据

cur.execute("select * from table")
arr=cur.fetchall()
pdres=pandas.DataFrame(arr, columns=['pricedate',hour', 'node','dart'])
pdres=pdres.set_index(['pricedate','hour','node'])

这是东西停止工作的地方...

pdres.pivot(index=['pricedate','hour'],columns='node',values='dart')

导致错误的项目数量传递了720,放置意味着2

这使我想到了这个问题 ,这促使我去做

pandas.pivot_table(pdres, index=['pricedate','hour'], columns='node', values='dart')

这告诉我“没有要聚合的数字类型

那导致我进入http://pandas.pydata.org/pandas-docs/dev/genic/pandas.DataFrame.convert_objects.html,但我不确定它的正确语法(何时使用),我也甚至不知道我走的路是否正确。

有两项更改可以使其起作用:

  • 首先不要set_index
  • 其次,不要使用convert_objects 一定要使用astype

具体来说,以下工作:

cur.execute("select * from table")
arr = cur.fetchall()
pdres = pandas.DataFrame(arr, columns=['pricedate', 'hour', 'node', 'dart'])
pdres['dart'] = pdres['dart'].astype(float)
pdres = pandas.pivot_table(pdres, index=['pricedate','hour'], columns='node', values='dart')

暂无
暂无

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

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