Toggle dark mode

Hi,

📢 Taipy v2.1 is about to be launched! 🚀 ETA:

Day(s)

:

Hour(s)

:

Minute(s)

:

Second(s)

M

Reply To: pandas query

Welcome to Taipy Forums Taipy GUI pandas query Reply To: pandas query

#237392
Florian JactaFlorian Jacta
Keymaster

Hi,

Here is a simple example of code to do some filtering on dates and string. If it is not enough, I can provide you with more complex example or I can give you access to the code of ‘Bar cutting”.


from taipy.gui import Gui 
import pandas as pd
import datetime as dt
import numpy as np

# this function will filter the dataframe
# we can have multiple functions to filter it depending on the
# visual elements that have been changed to be more efficient
def filter_data(data, input_str, start_date, end_date):
    index = (data['Date'] >  np.datetime64(start_date)) &\
            (data['Date'] <  np.datetime64(end_date)) &\
            (data["Value"].apply(lambda x: input_str in x))
    
    filtered_data = data[index]
    return filtered_data

# processing of a DataFrame (two columns: Date, Value)
original_data = pd.read_csv("graph_examples/airline_passengers.csv")
original_data['Date'] = pd.to_datetime(original_data['Date']) 
original_data['Value'] = original_data['Value'].astype(str)

# Initialization of variables of the GUI
input_for_filter = ""

start_date = dt.datetime(1950,1,1)
end_date = dt.datetime(1960,1,1)

filtered_data = filter_data(original_data,
                            input_for_filter,
                            start_date,
                            end_date)

# simple Markdown
md = """
<|{input_for_filter}|input|on_change=on_changer_filter|>

<|{start_date}|date|on_change=on_changer_filter|> <|{end_date}|date|on_change=on_changer_filter|>

<|{filtered_data}|table|>
"""

# function called by the GUI to filter the dataframe when a change is being done
# on visual elements
def on_changer_filter(state):
    state.filtered_data = filter_data(original_data,
                                      state.input_for_filter,
                                      state.start_date,
                                      state.end_date)
    
# GUI running
Gui(md).run()
    

I hope this will help you to create your own filtering. Don’t hesitate to come back to us.

Best regards,
Florian