Issue
I'm am using ipython widgets so unsure why this error is occurring
Using @interact and a list of strings as an input to the function below:
@wdg.interact(lines=["Number of cases", "Number of admissions", "Number of deaths"])
def time_series_graph( lines):
n_lines=len(lines)
if n_lines>0:
data[list(lines)].plot()
else:
print("Select the data you want to print.")
I already have a pandas data frame with the column names as in the list lines but this code does not work. The list of strings is split up so that each letter is a member of the list.
This (among others) is the error shown:
KeyError: "None of [Index(['N', 'u', 'm', 'b', 'e', 'r', ' ', 'o', 'f', ' ', 'c', 'a', 's', 'e',\n 's'],\n dtype='object')] are in the [columns]"
What is the reason for this and any idea how to fix it?
Thanks for your help!
Solution
Because you are using a dropdown selector, only one of your string choices is getting passed in, rather than a list of strings.
Hence,
lines
in the function scope is actually a string.list(lines)
produces a list of each character- Then trying to select those columns fails.
Try using a SelectMultiple instead? You can shift-click to select more than one.
import ipywidgets as wdg
@wdg.interact(lines=wdg.SelectMultiple(options=["Number of cases", "Number of admissions", "Number of deaths"]))
def time_series_graph(lines):
print(lines)
n_lines=len(lines)
if n_lines>0:
data[list(lines)].plot()
else:
print("Select the data you want to print.")
Answered By - ac24
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.