如何通过Dash回调从数据帧中获取计数

时间:2018-08-23 15:23:34

标签: python pandas numpy plotly plotly-dash

我想根据下拉菜单中的回调选择,在仪表板布局中的div中显示某些条件的计数。

我能够获得pandas数据框列中的值的下拉列表,但是我在弄清楚如何显示列中所选元素的总数时遇到了麻烦。

例如,我已经在Jupyter笔记本中编写了一个函数以进行计数

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变量的结果。

现在,我想根据下拉菜单中的选择在仪表板布局中执行相同的操作。我在这里使用了很多代码来开始Udemy课程:带Plotly和Dash的交互式Python仪表板

我从布局开始:

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)

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

def getCount(df, selected_org):

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

return html.H2(totCount)

请给我以下内容:

enter image description here

enter image description here

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

最终完整程序:

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()

1 个答案:

答案 0 :(得分:0)

您输入的内容是孩子,因此您应该返回一个列表,而不是像html.H2(your_count)一样;),因此解决方案是替换此功能

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)]