twelsh37

Pandas Dataframe and ipywidgets dropdown

Nov 30th, 2020 (edited)
574
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pandas as pd
  2. import numpy as np
  3. import ipywidgets as widgets
  4. from ipywidgets import Layout, AppLayout
  5. from IPython.display import display
  6. import functools
  7.  
  8. data = {'year': ['2000', '2000','2000','2000','2001','2001','2001','2001', '2002',  '2002', '2002', '2002',
  9.                  '2003','2003','2003','2003','2004', '2004','2004','2004', '2005', '2005', '2005', '2005',
  10.                  '2006', '2006', '2006', '2006', '2006', '2007', '2007', '2007', '2007', '2008', '2008', '2008', '2008',
  11.                  '2009', '2009', '2009', '2009'],
  12.        
  13.         'purpose':['Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business',
  14.                    'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday',
  15.                    'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study',
  16.                    'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday'
  17.                   ],
  18.        
  19.         'market':['Belgium', 'Luxembourg', 'France', 'Spain', 'Norway', 'Sweden', 'Germany', 'Austria', 'Denmark',
  20.                   'Portugal', 'Greece', 'Croatia', 'Belgium', 'Luxembourg', 'France', 'Spain', 'Norway', 'Sweden',
  21.                   'Germany', 'Austria', 'Denmark', 'Portugal', 'Greece', 'Croatia', 'Belgium', 'Luxembourg', 'France',
  22.                   'Spain', 'Norway', 'Sweden', 'Germany', 'Austria', 'Denmark', 'Portugal', 'Greece', 'Croatia', 'Belgium',
  23.                   'Luxembourg', 'France', 'Spain', 'Norway'
  24.                  ]}
  25.  
  26. df_london = pd.DataFrame (data, columns = ['year','purpose', 'market'])
  27.  
  28. # Get our unique values
  29. ALL = 'ALL'
  30. def unique_sorted_values_plus_ALL(array):
  31.     unique = array.unique().tolist()
  32.     unique.sort()
  33.     unique.insert(0, ALL)
  34.     return unique
  35.  
  36. output = widgets.Output()
  37.  
  38. # Dropdown listbox
  39. dropdown_year = widgets.Dropdown(description='Year',
  40.                                   options = unique_sorted_values_plus_ALL(df_london.year))
  41.  
  42. # Function to filter our dropdown listboxe
  43. def common_filtering(year):
  44.     df = df_london.copy()
  45.        
  46.     filters = []
  47.    
  48.     # Evaluate our dropdown listbox and return booleans for our selections
  49.     if year is not ALL:
  50.         filters.append(df['year'] == year)
  51.    
  52.     output.clear_output()
  53.    
  54.     with output:
  55.         if filters:
  56.             df_filter = functools.reduce(lambda x,y: x&y, filters)
  57.             display(df.loc[df_filter])
  58.         else:
  59.             display(df)
  60.  
  61. def dropdown_year_eventhandler(change):
  62.     common_filtering(change.new)
  63.  
  64. dropdown_year.observe(dropdown_year_eventhandler, names='value')
  65.  
  66. ui = widgets.HBox([dropdown_year])
  67.  
  68. display(ui, output)
RAW Paste Data