'Python Issue with @Interact
I am trying to run a interact Statement using Jupyter NoteBooks and getting an error in the return statement "NameError: name 'iris' is not defined".
!pip install ipywidgets
import numpy as np
import pandas as pd
import matplotlib.pyplot as ply
import seaborn as sns
import ipywidgets as widgets
from ipywidgets import interact, interact_manual
%cd c:\BuildYouFirstPythonAnalyticsSolution<----location of the CSV
IRIS = pd.read_csv('IRIS.CSV')
IRIS.head()
c:\BuildingYouFirstPythonAnalyticsSolution
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
@interact
def show_articles_more_than(column = 'sepal_lenght', x=5):
return iris.loc[iris[column] > x]
column
sepal_lenght
x
5
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\ipywidgets\widgets\interaction.py in update(self, *args)
255 value = widget.get_interact_value()
256 self.kwargs[widget._kwarg] = value
--> 257 self.result = self.f(**self.kwargs)
258 show_inline_matplotlib_plots()
259 if self.auto_display and self.result is not None:
C:\Users\MICHAE~1.STR\AppData\Local\Temp/ipykernel_25384/3043588339.py in show_articles_more_than(column, x)
1 @interact
2 def show_articles_more_than(column = 'sepal_lenght', x=5):
----> 3 return iris.loc[iris[column] > x]
NameError: name 'iris' is not defined
UPDATE:
The following code was the correct solution for the issue
%pip install ipywidgets
import numpy as np
import pandas as pd
import matplotlib.pyplot as ply
import seaborn as sns
import ipywidgets as widgets
from ipywidgets import interact, interact_manual
%cd c:/BuildingYouFirstPythonAnalyticsSolution/
iris = pd.read_csv('IRIS.CSV')
iris.head()
@interact
def show_articles_more_than(column = 'sepal_length', x=5):
return iris.loc[iris[column]>x]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
