首页 > 解决方案 > 散景列可按数字排序

问题描述

如何让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)

标签: python-3.xbokeh

解决方案


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

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

进入:

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

在此处输入图像描述


推荐阅读