Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- import ipywidgets as widgets
- from ipywidgets import Layout, AppLayout
- from IPython.display import display
- import functools
- # Initially, we will get the data and load it into a dataframe:
- # Updated data frame to load in our test data
- data = {'year': ['2000', '2000','2000','2000','2001','2001','2001','2001', '2002', '2002', '2002', '2002',
- '2003','2003','2003','2003','2004', '2004','2004','2004', '2005', '2005', '2005', '2005',
- '2006', '2006', '2006', '2006', '2006', '2007', '2007', '2007', '2007', '2008', '2008', '2008', '2008',
- '2009', '2009', '2009', '2009'],
- 'purpose':['Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business',
- 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday',
- 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study',
- 'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday'
- ],
- 'market':['Belgium', 'Luxembourg', 'France', 'Spain', 'Norway', 'Sweden', 'Germany', 'Austria', 'Denmark',
- 'Portugal', 'Greece', 'Croatia', 'Belgium', 'Luxembourg', 'France', 'Spain', 'Norway', 'Sweden',
- 'Germany', 'Austria', 'Denmark', 'Portugal', 'Greece', 'Croatia', 'Belgium', 'Luxembourg', 'France',
- 'Spain', 'Norway', 'Sweden', 'Germany', 'Austria', 'Denmark', 'Portugal', 'Greece', 'Croatia', 'Belgium',
- 'Luxembourg', 'France', 'Spain', 'Norway'
- ]}
- df_london = pd.DataFrame (data, columns = ['year','purpose', 'market'])
- #df_london = pd.read_csv(file, encoding = 'ISO-8859-1')
- # Example of when the dropdown year is selected a new text box is
- # displayed to show the purpose filter
- # Setup our List boxes to filter results based on filter choices
- ALL = 'ALL'
- def unique_sorted_values_plus_ALL(array):
- unique = array.unique().tolist()
- unique.sort()
- unique.insert(0, ALL)
- return unique
- # variable to store the common output for the dropdowns
- output = widgets.Output()
- # Here are the three dropdowns:
- dropdown_year = widgets.Dropdown(description='Year',
- options = unique_sorted_values_plus_ALL(df_london.year))
- dropdown_purpose = widgets.Dropdown(description='Purpose',
- options = unique_sorted_values_plus_ALL(df_london.purpose))
- dropdown_market = widgets.Dropdown(description='Market',
- options = unique_sorted_values_plus_ALL(df_london.market))
- # Function to filter our dropdown listboxes
- def common_filtering(year,purpose, market):
- # Make a copy of our data frame
- df = df_london.copy()
- # Initialise our empty list
- filters = []
- # Evaluate our dropdown list boxes and return booleans for our selections
- if year is not ALL:
- filters.append(df['year'] == year)
- if purpose is not ALL:
- filters.append(df['purpose'] == purpose)
- if market is not ALL:
- filters.append(df['market'] == market)
- # Ensure that we dont append our results to the cell
- # but overwrite the old results with the new
- output.clear_output()
- with output:
- if filters:
- # Create a pandas Array of True/False for each of teh entries
- df_filter = functools.reduce(lambda x,y: x&y, filters)
- # Using dataframe.loc show our filtered results
- display(df.loc[df_filter])
- else:
- # Otherwise display everything
- display(df)
- # We amend the event handlers to call the common_filtering function and pass the change.new value as well as the
- # current value of the other dropdown:
- def dropdown_year_eventhandler(change):
- common_filtering(change.new, dropdown_purpose.value, dropdown_market.value)
- def dropdown_purpose_eventhandler(change):
- common_filtering(dropdown_year.value, change.new, dropdown_market.value,)
- def dropdown_market_eventhandler(change):
- common_filtering(dropdown_year.value, dropdown_purpose.value, change.new)
- # We bind the handlers to the dropdowns, and that’s it!
- dropdown_year.observe(dropdown_year_eventhandler, names='value')
- dropdown_purpose.observe(dropdown_purpose_eventhandler, names='value')
- dropdown_market.observe(dropdown_market_eventhandler, names='value')
- # Setup our Hbox to display the list boxes in a line
- box_layout = widgets.Layout(display = 'inline-flex',
- flex_flow = 'row',
- align_items = 'stretch',
- border = 'solid',
- width = '100%')
- # Wrap up our widgets in a variable and assign them the box_layout style
- ui = widgets.HBox([dropdown_year, dropdown_purpose, dropdown_market], layout=box_layout)
- # Display our dropdown boxes and the output from the selection
- display(ui, output)
RAW Paste Data