DataFrame не может присваивать значения оси с Bokeh DataColumnSource - PullRequest
0 голосов
/ 18 апреля 2019

Я не понимаю, почему я не могу присвоить значения осям, я указал каждый столбец в источнике. Если кто-то может помочь мне, пожалуйста, я был бы признателен. Данные от 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)

Ответы [ 2 ]

1 голос
/ 18 апреля 2019

Я думаю, что вы хотите, чтобы это было:

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 должен содержать словари с ключами и векторными значениями, а не панды DataFrames.

Результат:

enter image description here

0 голосов
/ 19 апреля 2019

@ Тони

Я не видел необходимости в FOR LOOP, так как кадры данных - это диктат.Спасибо за руководство.Диктонары уже повторяются.

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)
...