gpt4 book ai didi

python - 值错误: expected a dict or pandas.数据帧

转载 作者:行者123 更新时间:2023-12-01 01:30:11 25 4
gpt4 key购买 nike

我正在尝试创建一个分类 vBar,它将显示为各种操作挑选的移民总数,但是,当我将“groupby”pandas 对象传递到列数据源时,我不断收到错误,并且我'我不太确定我做错了什么。

我在几个地方查找过类似的问题,但似乎找不到任何答案。

有人能指出我正确的方向吗?

#Imports
import pandas as pd
from bokeh.io import curdoc
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.models.tools import HoverTool
from bokeh.models import Button
from bokeh.layouts import row

#Global variables
viewFilter='Operation'

#Data
df = pd.read_csv('data.csv')
grouped = df.groupby(viewFilter)['Total Migrants']
source = ColumnDataSource(grouped)
#grouped = df.groupby(viewFilter)['Total Migrants'].sum()
#source = ColumnDataSource(pd.DataFrame(grouped))
operations = source.data[viewFilter].tolist()

# Fig Creation Function
def create_figure():
global viewFilter
p=figure(x_range=operations)
p.vbar(x=viewFilter, top='Total Migrants',
source=source, width=0.70)

p.title.text='Demo Chart'
p.xaxis.axis_label = viewFilter
p.yaxis.axis_label = 'Total Migrants'

#Hover took
hover = HoverTool()
hover.tooltips=[
("Total Migrants Rescued", "@{Total Migrants}")]
hover.mode='vline'
p.add_tools(hover)

return p

#Update Data with Ship-level aggregation
def shipUpdate():
print("Ship Button was Pushed")

#Widgets
shipButton = Button(label='Ship Level')
shipButton.on_click(shipUpdate)

#Implement Layout
layout = row(shipButton, create_figure())

#Add Layout to Document
curdoc().add_root(layout)

最佳答案

似乎如果我显式传入 pandas dataframe 对象,它就会解决此错误:

source = ColumnDataSource(pd.DataFrame(grouped))

关于python - 值错误: expected a dict or pandas.数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52953857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com