Python Forum

Full Version: how to filter data frame dynamically with the columns
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi every one I am learning python and am trying to filter a dataframe where I can create a dropdown for a column defined in dataframe and filter it dynamically . Please see the code below and i am able to create a dropdown for field id but it does not filter the values. If i hard code the (df2 = df1[(df1["id"]== 2)]). It does filter the data set. Can some one please help me and send me a sample code for filtering data in dataframe dynamically. Thanks

import pandas as pd
import ipywidgets as widgets
dummy_data1 = {
'id': ['1', '2', '3', '4', '5'],
'Feature1': ['A', 'C', 'E', 'G', 'I'],
'Feature2': ['B', 'D', 'F', 'H', 'J']}

df1 = pd.DataFrame(dummy_data1, columns = ['id', 'Feature1', 'Feature2'])

print (df1)

output = widgets.Output()

x = dropdown_field = widgets.Dropdown(options = (df1.id))
df2 = df1[(df1["id"]== x)]
input_widgets = widgets.HBox([dropdown_field])
display(input_widgets)

display(output)