Python 3.x DataFrame无法使用Bokeh DataColumnSource将值分配给axis
我不明白为什么我不能给轴赋值,我指定了源中的每一列。 如果有人能帮助我,我将不胜感激。 数据来自 (人口增长、生育率、预期寿命和死亡率) 一旦我可以将数据分配给轴,我将在绘图上做更多的工作,因此为什么会有这么多列Python 3.x DataFrame无法使用Bokeh DataColumnSource将值分配给axis,python-3.x,dataframe,plot,bokeh,Python 3.x,Dataframe,Plot,Bokeh,我不明白为什么我不能给轴赋值,我指定了源中的每一列。 如果有人能帮助我,我将不胜感激。 数据来自 (人口增长、生育率、预期寿命和死亡率) 一旦我可以将数据分配给轴,我将在绘图上做更多的工作,因此为什么会有这么多列 import pandas as pd from bokeh.io import output_file,show,output_notebook,push_notebook from bokeh.plotting import figure from bokeh.models imp
import pandas as pd
from bokeh.io import output_file,show,output_notebook,push_notebook
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource,HoverTool,CategoricalColorMapper
from bokeh.layouts import row,column,gridplot
from bokeh.models.widgets import Tabs,Panel
df = pd.read_csv('populationIndex2.csv', skiprows=1)
df = pd.DataFrame(df)
df.head()
df.columns
source = ColumnDataSource(data = dict(AF = df[(df['Unnamed: 1'] ==
'Africa') & (df['Series'] == 'Life expectancy at
birth for both sexes (years)')],
SA = df[(df['Unnamed: 1'] == 'South America') &
(df['Series'] == 'Life expectancy at birth for
both sexes (years)')],
NA = df[(df['Unnamed: 1'] == 'Northern America')
& (df['Series'] == 'Life expectancy at birth for
both sexes (years)')],
EU = df[(df['Unnamed: 1'] == 'Europe') &
(df['Series'] == 'Life expectancy at birth for
both sexes (years)')],
CA = df[(df['Unnamed: 1'] == 'Central America')
& (df['Series'] == 'Life expectancy at birth for
both sexes (years)')],
As = df[(df['Unnamed: 1'] == 'Asia') &
(df['Series'] == 'Life expectancy at birth for
both sexes (years)')],
Oc = df[(df['Unnamed: 1'] == 'Oceania') &
(df['Series'] == 'Life expectancy at birth for
both sexes (years)')],
Cb = df[(df['Unnamed: 1'] == 'Caribbean') &
(df['Series'] == 'Life expectancy at birth for
both sexes (years)')],
year = SA.Year))
tools = 'box_select, pan'
source.column_names
output_notebook()
p = figure(plot_height=300, plot_width=500,
title='Life expectancy by continent',
x_axis_label='Life expectancy by percent',
y_axis_label='Years',
tools=tools)
#p2 = figure(plot_height=300, plot_with=500,
# title='')
p.circle(x='AF', y='year', source = source, color='Yellow')
show(p)
我想你想要的是:
import pandas as pd
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
df = pd.read_csv('populationIndex2.csv', skiprows = 1)
for percent in df[(df['Unnamed: 1'] == 'Africa') & (df['Series'] == 'Life expectancy at birth for both sexes (years)')].values:
print percent
print percent [4]
source = ColumnDataSource(data = dict(AF = [percent[4] for percent in df[(df['Unnamed: 1'] == 'Africa') & (df['Series'] == 'Life expectancy at birth for both sexes (years)')].values],
year = df[(df['Unnamed: 1'] == 'Northern America') & (df['Series'] == 'Life expectancy at birth for both sexes (years)')].Year.values))
p = figure(plot_height = 300, plot_width = 500,
title = 'Life expectancy by continent',
y_axis_label = 'Life expectancy by percent',
x_axis_label = 'Years',
tools = 'box_select, pan')
p.circle(x = 'year' , y = 'AF', source = source, color = 'red')
show(p)
然后,您可以对数据框架内的其他国家应用相同的方法。列数据源
中的数据
应该包含带有键和向量值的字典,而不是数据帧
结果:
@Tony
我不认为需要for循环,因为数据帧本身就是dict。谢谢你的指导。口述已被重复
AF = df[(df['Unnamed: 1'] == 'Africa') &
(df['Series'] == 'Life expectancy at birth for
both sexes (years)')]
AfricaR = AF.Value.values
output: array(['53.7', '57.0', '60.2'],
dtype=object)
你能给我解释一下为什么for循环吗?
df[column].values
是一个由许多元素组成的数组。您要查找的百分比在该数组中位于第五位,因此percent[4]
谢谢@Tony我真的有点困惑,因为我使用iloc访问和分配变量,并用DataFrame绘制它们,但由于我尝试使用布尔值分配变量,所以我一直会得到这些错误。