Search code examples
pythonpandasdropdown

Fill ipywidget dropdown with content of CSV?


If I want to create a dropdown with ipywidget, I can do something like :

import ipywidgets as widgets

test = widgets.Dropdown(options=['hello', 'world'], value=None)
test

This code allow to put two choices in the dropdown : hello and world.

Now, I've these data :

Date,Value
2020-01-28,103424
2020-01-28,103424
2020-01-28,103424
2020-01-28,103424
2020-01-28,103424
2020-01-28,103424
2020-01-28,103424
2020-01-28,103424

I try to create two dropdowns : one with the content of the Date column and the other with the content of the Value columns :

import ipywidgets as widgets
import pandas as pd

df = pd.read_csv("/xxx/xxx/xxx/data.csv", usecols =["Date"])
df2 = pd.read_csv("/xxx/xxx/xxx/data.csv", usecols =["Value"])

d = widgets.Dropdown(options=[df])
d

d2 = widgets.Dropdown(options=[df2])
d2

But it doesn't work...

Do you have any ideas to do that ?


Solution

  • You need to specify the column/Series you need in the dropdown from your input dataframe.

    df = pd.read_csv("/xxx/xxx/xxx/data.csv", usecols =["Date", "Value"])
    
    d = widgets.Dropdown(options=df['Date'])
    d2 = widgets.Dropdown(options=df['Value'])
    display(d)
    display(d2)