【问题标题】:Customize Bokeh Unemployment Example: Replacing Percentage Value自定义散景失业示例:替换百分比值
【发布时间】:2019-03-13 17:59:55
【问题描述】:

起始码:https://docs.bokeh.org/en/latest/docs/gallery/texas.html

我正在尝试用我在 csv 文件中的不同百分比替换失业率。 csv 列是县名和浓度。

我对县数据使用与示例中相同的调用方法。只是为百分比值提取不同的数据。

我尝试将 csv 转换为字典,然后查找县名值并使用与起始代码相同的格式返回相应的浓度。我尝试过内连接、外连接、附加。我在这里错过了什么?

from bokeh.io import show
from bokeh.models import LogColorMapper
from bokeh.palettes import Viridis6 as palette
from bokeh.plotting import figure

from bokeh.sampledata.us_counties import data as counties

import pandas as pd
import csv

#with open('resources/concentration.csv', mode='r') as infile:
    #reader = csv.reader(infile)
        #with open('concentration_new.csv', mode='w') as outfile:
            #writer = csv.writer(outfile)
            #mydict = {rows[0]:rows[1] for rows in reader}

#d_1_2= dict(list(counties.items()) + list(mydict.items()))

pharmacy_concentration = []
with open('resources/unemployment.csv', mode = 'r') as infile:
    reader = csv.reader(infile, delimiter = ',', quotechar = ' ') # remove 
last attribute if you dont have '"' in your csv file
    for row in reader:
        name, concentration = row 
        pharmacy_concentration[name] = concentration

counties = {
    code: county for code, county in counties.items() if county["state"] == 
"tx"
}

palette.reverse()

county_xs = [county["lons"] for county in counties.values()]
county_ys = [county["lats"] for county in counties.values()]

county_names = [county['name'] for county in counties.values()]

#this is the line I am trying to have pull the corresponding value for the correct county
#county_rates = [d_1_2['concentration'] for county in counties.values()]
color_mapper = LogColorMapper(palette=palette)

data=dict(
    x=county_xs,
    y=county_ys,
    name=county_names,
    #rate=county_rates,
   )

   TOOLS = "pan,wheel_zoom,reset,hover,save"

   p = figure(
        title="Texas Pharmacy Concentration", tools=TOOLS,
        x_axis_location=None, y_axis_location=None,
        tooltips=[
            ("Name", "@name"), ("Pharmacy Concentration", "@rate%"), 
            (" (Long, Lat)", "($x, $y)")])
            p.grid.grid_line_color = None
            p.hover.point_policy = "follow_mouse"
            p.patches('x', 'y', source=data,
            fill_color={'field': 'rate', 'transform': color_mapper},
      fill_alpha=0.7, line_color="white", line_width=0.5)

show(p)

【问题讨论】:

    标签: python csv dictionary jupyter-notebook bokeh


    【解决方案1】:

    在不知道 csv 文件的确切结构的情况下很难推测。假设您的 csv 文件中只有 2 列:county_name + 浓度(没有第一个空列或介于两者之间)以下代码可能适合您:

    from bokeh.models import LogColorMapper
    from bokeh.palettes import Viridis256 as palette
    from bokeh.plotting import figure, show
    from bokeh.sampledata.us_counties import data as counties
    import csv
    
    pharmacy_concentration = {}
    with open('resources/concentration.csv', mode = 'r') as infile:
        reader = [row for row in csv.reader(infile.read().splitlines())]
        for row in reader:
            try:
                county_name, concentration = row  # add "dummy" before "county_name" if there is an empty column in the csv file
                pharmacy_concentration[county_name] = float(concentration)
            except Exception, error:
                print error, row
    
    counties = { code: county for code, county in counties.items() if county["state"] == "tx" }
    county_xs = [county["lons"] for county in counties.values()]
    county_ys = [county["lats"] for county in counties.values()]
    county_names = [county['name'] for county in counties.values()]
    county_pharmacy_concentration_rates = [pharmacy_concentration[counties[county]['name']] for county in counties if counties[county]['name'] in pharmacy_concentration]
    palette.reverse()
    color_mapper = LogColorMapper(palette = palette)
    
    data = dict(x = county_xs, y = county_ys, name = county_names, rate = county_pharmacy_concentration_rates)
    p = figure(title = "Texas Pharmacy Concentration, 2009", tools = "pan,wheel_zoom,reset,hover,save", tooltips = [("Name", "@name"), ("Pharmacy Concentration)", "@rate%"), ("(Long, Lat)", "($x, $y)")], x_axis_location = None, y_axis_location = None,)
    p.grid.grid_line_color = None
    p.hover.point_policy = "follow_mouse"
    p.patches('x', 'y', source = data, fill_color = {'field': 'rate', 'transform': color_mapper}, fill_alpha = 0.7, line_color = "white", line_width = 0.5)
    
    show(p)
    

    结果:

    【讨论】:

    • csv 只是 2 列,没有额外的列,有一个名称列和一个带有相应标题的百分比列:名称浓度 Anderson 20.8 等。我收到一个 valueerror 说无法将字符串转换为浮点数:'浓度'。
    • csv 阅读器将百分比值作为字符串值提取,由于某种原因,float 命令抛出该错误无法将字符串转换为浮点数。
    • 我在代码中添加了 next(infile) 以便 csv 解析从第二行开始并跳过标题。
    • 对于county_pharmacy_concentration_rates,它返回县名作为keyerror。有没有办法将 csv 中的浓度值加入/连接到县字典中,以县名为索引?
    • 字典是这样设置的:{(48, 1): {'name': 'Anderson', 'detailed name': 'Anderson County, Texas', 'state': 'tx ', 'lats': [31.91362, 现在我正试图找到一种方法将安德森的浓度值附加到它的字典中。然后我可以在 County.values() 中调用县的浓度。
    猜你喜欢
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 2015-12-10
    • 2019-01-14
    • 2015-06-21
    • 2023-04-02
    相关资源
    最近更新 更多