Filter dataframe by selections made in select box

Really sorry for the late response, been occupied with other things.

Pre-requisite:

  1. I have shaped my data like this to be able to filter through it.
  2. I will then reshape the dataframe into a new dataframe using the pd.pivot_table function to create columns from the row data from a column, called ‘Data’ in my data and subsequent values to populate them based on the filtered choices chosen in the select boxes.

So this is how my code works:

    countries = data.Geography.unique() # filter a column called Geography
    # first data filtering choice by country
    Country_choice = Data_filtering[0].selectbox("Geography", countries) # populate the result in a selectbox to choose data from
    # CATEGORY - get all row values in the category column that are in the country column
    category = data['Category'].loc[data['Geography'] == Country_choice].unique()  
    Category_choice = Data_filtering[1].selectbox("Category", category)
    # SERIES - get all series row values that are in the category column
    series = data.Series.loc[data['Category']==Category_choice].unique()
    Series_choice = Data_filtering[2].radio('Sequential Data type', series)


    # filter another column called 'Data' which I will use as columns for the reshaped dataframe to display the dataframe and then chart the selected data. Here the 'Data' column will be displayed based on the choices made in the above. 
    data_col = data['Data'][(data['Geography']==Country_choice) & (data['Category']==Category_choice) & (data['Series']==Series_choice) & (data['Data Type']==Data_type_choice)].unique() 

    # Create a pivot table to reshape the dataframe into a 2d dataframe which I can use the data_col variable choices to select the filtered data from. 
    Trans_data=data.pivot_table(index='Date', columns='Data', values='Values').rename_axis(None, axis=1)

You can also reshape your data if its displayed as my previous post here.

asehmi providers a wonderful solution for data structured as was so in the picture I displayed in that post. But the code should work in the same way.

1 Like