【问题标题】:Trouble loading csv data into bokeh无法将 csv 数据加载到散景中
【发布时间】:2019-04-09 13:26:43
【问题描述】:

我在将数据从 csv 文件加载到 bokeh 时遇到问题,从 bokeh 数据库它可以工作,但是当我尝试从 csv 文件时它没有加载,所以我一直在阅读但到目前为止没有运气。 提前致谢

df = pd.read_csv('unemployment1948.csv', delimiter = ',', index_col = 
                 'Year')
df = pd.DataFrame(df)
df.head()


output_notebook()
group = df[35:].groupby('Year')
source = ColumnDataSource(df)
group.describe()
df.columns
#source = ColumnDataSource(df(x=df.loc[15:40].index, 
#                              y=df.loc[15:40].Annual))

p = figure(plot_height=300, plot_width=900, x_range=group, 
           title='Umployment over the years',
           x_axis_label='Year', y_axis_label='Annual')

p.circle(x=index, y='Annual', width=0.9, color ='#35B778' , source=source)
show(p)

【问题讨论】:

    标签: python-3.x csv plot bokeh


    【解决方案1】:

    您的df 已经是熊猫DataFrame。试试这个:

    import os
    import pandas as pd
    from bokeh.io import show
    from bokeh.models import ColumnDataSource
    from bokeh.plotting import figure
    
    df = pd.read_csv(os.path.join(os.path.dirname(__file__), "unemployment1948.csv",))
    output_notebook()
    source = ColumnDataSource(df)
    
    p = figure(plot_height = 300, plot_width = 900,
               title = 'Umployment over the years',
               x_axis_label = 'Year', y_axis_label = 'Annual')
    
    p.circle(x = 'Year', y = 'Annual', line_width = 0.9, color = '#35B778' , source = source)
    show(p)
    

    【讨论】:

      【解决方案2】:

      我学习了如何处理 DataFrame 和 ColumnDateSource,因此您可以轻松地操作数据,并且不需要使用 OS 模块。 感谢您的帮助。

      import csv
      #import re
      import pandas as pd
      import numpy as np
      #import random
      ##from collections import Counter, defaultdict
      #import random
      
      dfU = pd.read_csv('unemployment1948.csv')
      dfU = pd.DataFrame(dfU)
      dfU.index
      y = dfU.iloc[35:].Year
      x = dfU.iloc[35:].Annual
      #xRange = dfU.iloc[35:]
      source = ColumnDataSource(dfU)
      
      output_notebook()
      p = figure(plot_height=300, plot_width=800, title='Unemployment over the years')
      p.vbar(x='Year', top='Annual', width=0.9, color ='#35B778' , source=source)
      show(p)
      

      【讨论】:

        猜你喜欢
        • 2014-05-25
        • 1970-01-01
        • 1970-01-01
        • 2018-08-23
        • 1970-01-01
        • 1970-01-01
        • 2020-10-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多