Pat
Pat

Reputation: 39

Specific data from a dataframe based on date in another column

@app.callback(
    Output('stats', 'children'),
    Input('picker_main', 'date'))
def update_table(date_value):
    table = {}
    for query_id in queries_daily:
        df_temp = data_manager.data[query_id]
        df_temp.set_index('day')
        try:
            table[query_id] = df_temp[query_id].where(df_temp['day'] == datetime.strptime(date_value, "%Y-%m-%d").date())
            #table[query_id] = df_temp.loc["day", query_id].where(df_temp['day'] == temp)

            #table[query_id] = df_temp[df_temp["day"] == temp]
        except Exception as e:
            table[query_id] = 0
            print(e)

I'm trying to get a row from the dateframe and store it in a dictionary or another dateframe.

It's actually only about this bit:

table[query_id] = df_temp[query_id].where(df_temp['day'] == datetime.strptime(date_value, "%Y-%m-%d").date())

table --> empty dict

df_temp --> df with 2 columns - first with name in variable "query_id" and second with date. From which I'm trying to get value stored in the column named with "query_id" along with the "query_id" keyword.

I've been trying also converting date to string format and using dataframe instead of an empty dictionary

It doesn't return any data. I posted a longer piece of code at first as was wondering if maybe someone spot that I can do something in a better way

Thanks!

Upvotes: 2

Views: 48

Answers (1)

rafaelc
rafaelc

Reputation: 59274

Seems like you need query

table = { query_id : data_manager.data[query_id]\
                                 .query(f'day == "{date_value}"')['day']
         for query_id in queries_daily }

Upvotes: 2

Related Questions