散景列可按数字排序

时间:2019-05-14 17:40:02

标签: python-3.x bokeh

如何获取bokeh表以浮点值而不是字符串值对列进行排序?我有浮点数据,该列将继续作为字符串进行排序,因此无法正确地按升序/降序排序。随附了数据和代码段。

数据:

GB, 15.789, /path/to/A, size, 100.0
GB, 600.123, /path/to/B, size, 100.0
GB, 70.456, /path/to/C, size, 100.0

摘要:

#!/usr/bin/env python3

from bokeh.io import output_file, show, save
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn, NumberFormatter

def get_data():
    lines = open('json.tmp', 'r').read().splitlines()

    data = { 
        'unit': [], 
        'value': [], 
        'location': [], 
        'type': [], 
        'tol': [], 
    }   

    for line in lines:
        words = line.split(',')
        data['unit'].append(words[0])
        data['value'].append(words[1])
        data['location'].append(words[2])
        data['type'].append(words[3])
        data['tol'].append(words[4])

    return data

if __name__ == "__main__":
    data = get_data()

    output_file("data_table.html")
    source = ColumnDataSource(data)
    columns = [ 
        TableColumn(field="location", title="Path"),
        TableColumn(field="value", title="Value", formatter=NumberFormatter(format="0.0")),
        TableColumn(field="unit", title="Unit", width=10),
        TableColumn(field="type", title="Type", width=20)
    ]   

    data_table = DataTable(source=source, columns=columns)
    save(data_table)

1 个答案:

答案 0 :(得分:0)

这是因为您将字符串而不是浮点数传递给ColumnDataSource。 更改行:

data['value'].append(words[1])

进入:

data['value'].append(float(words[1]))

enter image description here