Имею подобный код с формированием таблицы и фильтрацией из выпадающих списков,
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
import pandas as pd
df = pd.read_csv('https://gist.githubusercontent.com/chriddyp/'
'c78bf172206ce24f77d6363a2d754b59/raw/'
'c353e8ef842413cae56ae3920b8fd78468aa4cb2/'
'usa-agricultural-exports-2011.csv')
def generate_table(dataframe, max_rows=10):
return html.Table(
# Header
[html.Tr([html.Th(col) for col in dataframe.columns])] +
# Body
[html.Tr([
html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
]) for i in range(min(len(dataframe), max_rows))]
)
app = dash.Dash()
app.layout = html.Div(children=[
dcc.Dropdown(id='dropdown', options=[
{'label': i, 'value': i} for i in df.state.unique()
], multi=True, placeholder='Filter by state...'),
dcc.Dropdown(id='dropdown1', options=[
{'label': i, 'value': i} for i in df.corn.unique()
], multi=True, placeholder='Filter by corn...'),
dcc.Dropdown(options=[
{'label': i, 'value': i} for i in df.wheat.unique()
], multi=True, placeholder='Filter by wheat...'),
html.Div(id='table-container', style={"margin": "20px"})
], style={"margin": "100px"})
@app.callback(
dash.dependencies.Output('table-container', 'children'),
dash.dependencies.Input('dropdown', 'value'),
dash.dependencies.Input('dropdown1', 'value'))
def display_table(dropdown_value, dropdown_value1):
if dropdown_value is None and dropdown_value1 is None:
return generate_table(df)
if dropdown_value is not None and dropdown_value1 is not None:
dff = df.loc[(df.state.str.contains('|'.join(dropdown_value))) & (df.corn.str.contains('|'.join(dropdown_value1)))]
return generate_table(dff)
if dropdown_value is not None:
dff = df.loc[df.state.str.contains('|'.join(dropdown_value))]
return generate_table(dff)
if dropdown_value1 is not None:
dff = df.loc[df.corn.str.contains('|'.join(dropdown_value1))]
return generate_table(dff)
if __name__ == '__main__':
app.run_server(debug=True)
при добавление фильтрации по колонке corn
сталкиваюсь с ошибкой "AttributeError: Can only use .str accessor with string values!"
Просьба, подсказать, как решить ее в данном случае? также, хотелось спросить, возможно есть другой способ подобной фильтрации без громоздких конструкций с if?