在网页中显示熊猫数据框,并使用破折号动态过滤

问题描述 投票:1回答:1

我正在尝试使用Dash显示数据框。我拥有的数据帧是https://www.kaggle.com/timoboz/superbowl-history-1967-2020。我的目标是使用一个搜索按钮在网页上显示数据框,该按钮可动态搜索所有列并过滤数据框。

到目前为止,我有以下显示数据框的代码。

import pandas as pd
import dash
import dash_table
from dash.dependencies import Input, Output
df  = pd.read_csv('./Data/superbowl.csv')
PAGE_SIZE = 10
app = dash.Dash(__name__)

app.layout = dash_table.DataTable(
    id='datatable-paging',
    columns=[
        {"name": i, "id": i} for i in df.columns #sorted(df.columns)
    ],
    page_current=0,
    page_size=PAGE_SIZE,
    page_action='custom',

    sort_action='custom',
    sort_mode='single',
    sort_by=[]
)


@app.callback(
    Output('datatable-paging', 'data'),
    [Input('datatable-paging', "page_current"),
     Input('datatable-paging', "page_size"),
     Input('datatable-paging', 'sort_by')])
def update_table(page_current,page_size,sort_by):
    if len(sort_by):
        dff = df.sort_values(
            sort_by[0]['column_id'],
            ascending=sort_by[0]['direction'] == 'asc',
            inplace=False
        )
    else:
        # No sort is applied
        dff = df

    return dff.iloc[
           page_current * page_size:(page_current + 1) * page_size
           ].to_dict('records')



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

Output so far阅读了文档https://dash.plot.ly/datatable/callbacks特别是“带过滤的后端分页”之后,我找不到一种方法可以像单个文本框那样搜索所有列并为匹配的条目过滤数据框。

python-3.x pandas plotly plotly-dash
1个回答
0
投票

因此,最好的方法是将输入组件用于搜索查询。然后,可以通过执行熊猫过滤器来更新表格。这将返回所有单元格包含文本的所有行。

© www.soinside.com 2019 - 2024. All rights reserved.