基于日期的破折号过滤器 df [英] Dash filter df based on dates

查看:72
本文介绍了基于日期的破折号过滤器 df的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢根据日期过滤我的 df.我的目标是拥有一个日历,我可以从中选择日期并根据所选日期,df 将过滤结果,即相同值的总和(在本例中为名称").这是我的 df 的样子.

I like to filter my df based on dates. What I'm aiming for is to have a calendar wherein I can choose the dates from and based on the dates chosen, the df will filter the result, which is the sum of the same values (in this case, 'Name'). Here's how my df looks like.

这是我的代码:

from datetime import datetime as dt
import dash
from dash.dependencies import Input, Output, State
import dash_table
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd

raw = [['tom', 10, pd.to_datetime('2021-01-01')], ['tom', 5, pd.to_datetime('2021-01-01')], ['nick', 15, pd.to_datetime('2021-01-01')], ['nick', 8, pd.to_datetime('2021-01-01')], ['juli', 14, pd.to_datetime('2021-01-01')],['juli', 7, pd.to_datetime('2021-01-01')],
       ['tom', 8, pd.to_datetime('2021-01-02')], ['tom', 3, pd.to_datetime('2021-01-02')], ['nick', 4, pd.to_datetime('2021-01-02')], ['nick', 1, pd.to_datetime('2021-01-02')],['juli', 12, pd.to_datetime('2021-01-02')], ['juli', 6, pd.to_datetime('2021-01-02')]]
df = pd.DataFrame(raw, columns=['Name', 'Apples Gathered', 'date'])
df['date'] = df['date'].dt.strftime('%Y-%m-%d')

#App layout
app = dash.Dash(__name__, prevent_initial_callbacks=True, suppress_callback_exceptions=True)

app.layout = html.Div([
    dcc.DatePickerRange(
        id='datepicker',
        display_format='DD-MM-YYYY',
        first_day_of_week=1,
        min_date_allowed=dt(2021, 1, 1),
        max_date_allowed=dt(2021,1,5),
    ),
])
dash_table.DataTable(
       id='datatable',
       columns=[
            {'id': 'Name', 'name': 'Name'},
            {'id': 'Apples Gathered', 'name': 'Apples Gathered'},
            {'id': 'date', 'name': 'date'}],

        data = df.to_dict('records'),
)

@app.callback(
       [Output('datatable', 'data'), Output('datatable', 'columns')],
       [Input('datepicker', 'start_date'),
       Input('date-picker', 'end_date')]
)
def update_table(start_date, end_date):
    #dff = [(df['date'].dt.date.astype(str) > start_date) & (df['date'].dt.date.astype(str) < end_date)]
    data = (df.groupby(['Name']).sum())
    fdata = data.to_dict('records')
    columns = [
            {'id': 'Name', 'name': 'Name'},
            {'id': 'Apples Gathered', 'name': 'Apples Gathered'}]
    return fdata, columns

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

当我运行该代码时,只有日历出现,我的 df 没有.如何修复回调函数,以便我的 df 也会显示出来,从而可以根据日期进行过滤.

When I run that code, only the calendar comes up, my df doesn't. How do I fix the callback function so that my df would show up as well, which result I can filter based on the dates.

推荐答案

dash_table.DataTable(
    id='datatable',
    columns=[
        {'id': 'Name', 'name': 'Name'},
        {'id': 'Apples Gathered', 'name': 'Apples Gathered'},
        {'id': 'date', 'name': 'date'}],

    data=df.to_dict('records')
)

layout 之外定义.例如,将其复制并粘贴到 html.Div 元素中应该会显示在您的应用中.

is defined outside of the layout. Copying and pasting it into the html.Div element for example should display it in your app.

app.layout = html.Div([
    dcc.DatePickerRange(
        id='datepicker',
        display_format='DD-MM-YYYY',
        first_day_of_week=1,
        min_date_allowed=dt(2021, 1, 1),
        max_date_allowed=dt(2021,1,5),
    ),

    dash_table.DataTable(
        id='datatable',
        columns=[
            {'id': 'Name', 'name': 'Name'},
            {'id': 'Apples Gathered', 'name': 'Apples Gathered'},
            {'id': 'date', 'name': 'date'}],

        data=df.to_dict('records')
    )
])

这篇关于基于日期的破折号过滤器 df的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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