如何从 Dash 回调的数据帧中获取计数 [英] How to get the count from a dataframe from a Dash Callback

查看:53
本文介绍了如何从 Dash 回调的数据帧中获取计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据下拉菜单中的回调选择在我的破折号布局中的 div 内显示某些条件的计数.

I'd like to display the count of certain criteria inside a div in my dash layout based off callback selection from dropdown.

我能够获得 Pandas 数据框列中值的下拉列表,但我无法弄清楚如何显示该列的选定元素的总数.

I'm able to get the dropdown for the values in a pandas dataframe column, but I'm having trouble figuring out how to display the total count of the a selected element of the column.

例如,我在 Jupyter notebook 中编写了一个函数来获取计数

For example, I've written a function in Jupyter notebook to get a count

def getCount(df, selected_org):
    totCount = df[df['ORGANIZATIONS'] == selected_org].AGENCY.count()
    return count

selected_org = 'REGION 1'

getCount(df, selected_org)

显示值为:3187

这个值是我的 selected_org 变量的结果.

This value is the result of my selected_org variable.

现在,我想根据下拉列表中的选择在我的仪表板布局中做同样的事情.我使用了这里的大部分代码来开始 Udemy 课程:Interactive Python Dashboards with Plotly and Dash

Now, I'd like to do the same thing in my dash layout based off the selection from dropdown. I used much of the code here to get started from Udemy course: Interactive Python Dashboards with Plotly and Dash

我从我的布局开始:

org_options = []
for org in df['ORG_CODE_LEVEL_2_SHORT'].unique():
org_options.append({'label': str(org), 'value': org})

app.layout = html.Div(children=[

html.Label('Select Org:'),
    dcc.Dropdown(id='org-dropdown', options=org_options, 
    value=df['ORG_CODE_LEVEL_2_SHORT'].min()),
html.P(html.Div(id='container'))

然后我回电:

@app.callback(
    Output(component_id='container', component_property='children'), 
[Input(component_id='org-dropdown', component_property='value')])
def update_table(selected_org):
    filtered_org = df[df['ORG_CODE_LEVEL_2_SHORT'] == selected_org]
    return getCount(df, filtered_org)

还有一个生成计数的函数:

And a function to generate the count:

def getCount(df, selected_org):

    totCount = df[df['ORG_CODE_LEVEL_2_SHORT'] == 
               selected_org].AGENCY_INFO_5.count()

return html.H2(totCount)

这给了我以下内容:

但它并没有给我计数.任何帮助表示赞赏.

But it isn't giving me the count. Any help is appreciated.

最终完整的程序:

import os
import glob
import pandas as pd
import numpy as np
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

app = dash.Dash()

# DATA FROM EEPROFILE
path = r'mydata'
extension = 'xlsx'
os.chdir(path)
files = [i for i in glob.glob('*.{}'.format(extension))]
latest_file = max(files, key=os.path.getctime)
df = pd.DataFrame()
eeprofi = pd.read_excel(latest_file, converters={'ORG_CODE_LEVEL_3': str})
df = df.append(eeprofi)

def getCount(df, selected_org):

    totCount = df[df['ORG_CODE_LEVEL_2_SHORT'] == selected_org].AGENCY_INFO_5.count()

    return html.H2(totCount)

org_options = []
for org in df['ORG_CODE_LEVEL_2_SHORT'].unique():
    org_options.append({'label': str(org), 'value': org})

app.layout = html.Div(children=[

    html.Label('Select Org:'),
    dcc.Dropdown(id='org-dropdown', options=org_options, value=df['ORG_CODE_LEVEL_2_SHORT'].min()),
    html.P(html.Div(id='container'))

])

@app.callback(
    Output(component_id='container', component_property='children'), [Input(component_id='org-dropdown', component_property='value')])
def update_table(selected_org):
    filtered_org = df[df['ORG_CODE_LEVEL_2_SHORT'] == selected_org]
    return getCount(df, filtered_org)


if __name__ == '__main__':
    app.run_server()

推荐答案

你的输入是一个孩子,所以你应该返回一个列表,而不是像你那样返回 html.H2(your_count) ;) 所以解决方案是替换这个函数

Your input is a children so you should return a list and not html.H2(your_count) like you did ;) so the solution is to replace this function

def getCount(df, selected_org):

    totCount = df[df['ORG_CODE_LEVEL_2_SHORT'] == selected_org].AGENCY_INFO_5.count()

    return html.H2(totCount)

通过这个:

def getCount(df, selected_org):

    totCount = df[df['ORG_CODE_LEVEL_2_SHORT'] == selected_org].AGENCY_INFO_5.count()

    return [html.H2(totCount)]

这篇关于如何从 Dash 回调的数据帧中获取计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆