Dropdown Ipywidgets for a particular dataset

Viewed 73

I have a dataset 'unique_words_df' with three columns "postal code", word_count" and 'unique_word'.

enter image description here

I want to create a dropdown ipy-widget using the "postal_code" as the dropdown and then I want to plot a count-plot with 'unique_word' on the x-axis. Can anyone give an idea. I tried but it is not working. Error shown is shown below. But the temp data frame looks fine to me.

def count_plotter(col = 'postal_code'):
temp = unique_words_df.groupby(col)
sns.countplot(x='unique_word', data=temp)
return temp

col_selector =  widgets.Dropdown(
    options=list(unique_words_df['postal_code'].unique()),
    value='32822',
    description='PINCODE')

interact(count_plotter, col = col_selector)

Error:

~/opt/anaconda3/lib/python3.8/site-packages/ipywidgets/widgets/interaction.py in update(self, *args)
    254                     value = widget.get_interact_value()
    255                     self.kwargs[widget._kwarg] = value
--> 256                 self.result = self.f(**self.kwargs)
    257                 show_inline_matplotlib_plots()
    258                 if self.auto_display and self.result is not None:

<ipython-input-41-b7e91f1f6779> in count_plot(col)
      1 def count_plot(col = 'postal_code'):
----> 2     temp = unique_words_df.groupby(col)
      3     sns.countplot(x='unique_word', data=temp)
      4     return temp
      5 col_selector =  widgets.Dropdown(

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in groupby(self, by, axis, level, as_index, sort, group_keys, squeeze, observed, dropna)
   6509         axis = self._get_axis_number(axis)
   6510 
-> 6511         return DataFrameGroupBy(
   6512             obj=self,
   6513             keys=by,

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/groupby/groupby.py in __init__(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, squeeze, observed, mutated, dropna)
    523             from pandas.core.groupby.grouper import get_grouper
    524 
--> 525             grouper, exclusions, obj = get_grouper(
    526                 obj,
    527                 keys,

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/groupby/grouper.py in get_grouper(obj, key, axis, level, sort, observed, mutated, validate, dropna)
    779                 in_axis, name, level, gpr = False, None, gpr, None
    780             else:
--> 781                 raise KeyError(gpr)
    782         elif isinstance(gpr, Grouper) and gpr.key is not None:
    783             # Add key to exclusions

KeyError: '32822'

Out[41]:
<function __main__.count_plot(col='postal_code')>```
1 Answers

Your code is telling you the answer. You have asked for the value to be All, but you have added ALL as the only other option. The first is title case, and the second is upper case. Set value='ALL' should work.

Related