How to st.datafreame filter data transfer to new pandas dataframe

I have observed that my code functions correctly and filters data according to user selection. However, I would like to attempt transferring the user-selected or filtered data to a new pandas DataFrame. Unfortunately, the data is not being transferred to the new DataFrame, nor is the selected filtered data being converted into a list for use in the new pandas DataFrame. I would appreciate any ideas and the correct code to achieve this.

this is my code

import streamlit as st
import numpy as np
import pandas as pd
from streamlit_dynamic_filters import DynamicFilters

data = {
‘region’: [‘North America’, ‘North America’, ‘Europe’, ‘Oceania’,
‘North America’, ‘North America’, ‘Europe’, ‘Oceania’,
‘North America’, ‘North America’, ‘Europe’, ‘Oceania’],
‘country’: [‘USA’, ‘Canada’, ‘UK’, ‘Australia’,
‘USA’, ‘Canada’, ‘UK’, ‘Australia’,
‘USA’, ‘Canada’, ‘UK’, ‘Australia’],
‘city’: [‘New York’, ‘Toronto’, ‘London’, ‘Sydney’,
‘New York’, ‘Toronto’, ‘London’, ‘Sydney’,
‘New York’, ‘Toronto’, ‘London’, ‘Sydney’],
‘district’: [‘Manhattan’, ‘Downtown’, ‘Westminster’, ‘CBD’,
‘Brooklyn’, ‘Midtown’, ‘Kensington’, ‘Circular Quay’,
‘Queens’, ‘Uptown’, ‘Camden’, ‘Bondi’]
}

df = pd.DataFrame(data)

dynamic_filters = DynamicFilters(df, filters=[‘region’, ‘country’, ‘city’, ‘district’])

st.write(“Apply filters in any order :backhand_index_pointing_down:”)

dynamic_filters.display_filters(location=‘columns’, num_columns=2, gap=‘large’)
dynamic_filters.display_df()

st.write(‘Testing for treanser data to new datafreame’)
if st.button(‘Check availability’):
df2 = pd.DataFrame(dynamic_filters.display_df())
st.write(df2) # did not show the filter jor selected data show blanck dataframe

this selected data transfer new pandas dataframe

From the component’s open-source code, .display_df() calls the st.dataframe command and does not return the filtered dataframe: streamlit-dynamic-filters/streamlit-dynamic-filters/dynamic_filters.py at 5810fea03203de5ad8a8e0cc71a3a000333097f1 · arsentievalex/streamlit-dynamic-filters · GitHub

You need the .filter_df() method to get the filtered dataframe.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.