python - 下拉排序

标签 python pandas plotly-dash

我正在使用 csv 文件中的唯一值填充下拉列表,但这些值未排序。 我尝试使用 listname.sort() 但收到此错误消息:

TypeError: '<' not supported between instances of 'dict' and 'dict'

代码是:

df = pd.read_csv('Data/ItemList.csv', delimiter=',', encoding="utf-8-sig")
options = []
for item in df['Item Name'].unique():
    options.append({'label':str(item),'value':item})
soptions = options.sort()

html.Div([
    html.H3('Select crime :', style={'paddingRight':'30px'}),
    dcc.Dropdown(
        id='my_crime_picker',
        options=soptions,
        value=['Burglery'],
        multi=True
    )
], style={'display':'inline-block', 'verticalAlign':'top', 'width':'30%'})

我希望这对于 Python 专家来说是一件简单的事情。

最佳答案

尝试像这样定义选项:

options = [{'label': i, 'value': i} for i in df['Item Name'].unique().sort_values()]

所以你修改后的代码将是:

df = pd.read_csv('Data/ItemList.csv', delimiter=',', encoding="utf-8-sig")

html.Div([
    html.H3('Select crime :', style={'paddingRight':'30px'}),
    dcc.Dropdown(
        id='my_crime_picker',
        options=[{'label': i, 'value': i} for i in df['Item Name'].unique().sort()],
        value=['Burglary'],
        multi=True
    )
], style={'display':'inline-block', 'verticalAlign':'top', 'width':'30%'})

关于python - 下拉排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53770415/

相关文章:

已关闭文件的 Python ValueError I/O 操作

python - 将 Graphlab SFrame 日期列拆分为三列(年月日)

python - Django 过滤器 JSONField 字典列表

Python 3 正则表达式 $ 但不是字符串中的 $$

python - 根据 pandas 数据框中的日期时间选择数据

python - 如何正确添加样式到我的 Dash 应用程序?

python - 在 Plotly 的单个跟踪中显示多个图例

python - 如何为 Plotly Dash 回调函数编写测试用例?

c++ - 使用python boost 编译错误

python - 为 TensorFlow reshape Gym 数组