DataFrame无法使用Bokeh DataColumnSource为轴分配值

问题描述 投票:0回答:2

我不明白为什么我不能将值分配给轴,我指定了源中的每一列。如果有人可以帮助我,我将不胜感激。数据来自http://data.un.org/(人口增长,生育率,预期寿命和死亡率)一旦我可以将数据分配给轴,我将更多地在绘图上工作,因此为什么这么多列。

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)
python-3.x dataframe plot bokeh
2个回答
1
投票

我想你想要的是这个:

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)

然后,您可以对数据框内的其他国家/地区应用相同的方法。 data中的ColumnDataSource应该包含带键和矢量值的字典而不是pandas DataFrames

结果:

enter image description here


0
投票

@Tony

我没有看到需要FOR LOOP,因为数据帧本身就是一个字典。感谢您的指导。 Dictonaries已经迭代了。

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)
© www.soinside.com 2019 - 2024. All rights reserved.