Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Erro ao tentar gerar boxplot

Boa tarde.

Pessoal, estou com um erro ao tentar visualizar o conteúdo da variável medias_por_filmes utilizando o seaborn. Sigo os mesmos passos da aula 2, mas está com erro:

Insira aqui a descrição dessa imagem para ajudar na acessibilidade

sns.boxplot(medias_por_filmes)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File ~\anaconda3\Lib\site-packages\pandas\core\indexes\base.py:3653, in Index.get_loc(self, key)
   3652 try:
-> 3653     return self._engine.get_loc(casted_key)
   3654 except KeyError as err:

File ~\anaconda3\Lib\site-packages\pandas\_libs\index.pyx:147, in pandas._libs.index.IndexEngine.get_loc()

File ~\anaconda3\Lib\site-packages\pandas\_libs\index.pyx:176, in pandas._libs.index.IndexEngine.get_loc()

File pandas\_libs\hashtable_class_helper.pxi:2606, in pandas._libs.hashtable.Int64HashTable.get_item()

File pandas\_libs\hashtable_class_helper.pxi:2630, in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
Cell In[43], line 1
----> 1 sns.boxplot(medias_por_filmes)

File ~\anaconda3\Lib\site-packages\seaborn\categorical.py:2231, in boxplot(data, x, y, hue, order, hue_order, orient, color, palette, saturation, width, dodge, fliersize, linewidth, whis, ax, **kwargs)
   2224 def boxplot(
   2225     data=None, *, x=None, y=None, hue=None, order=None, hue_order=None,
   2226     orient=None, color=None, palette=None, saturation=.75, width=.8,
   2227     dodge=True, fliersize=5, linewidth=None, whis=1.5, ax=None,
   2228     **kwargs
   2229 ):
-> 2231     plotter = _BoxPlotter(x, y, hue, data, order, hue_order,
   2232                           orient, color, palette, saturation,
   2233                           width, dodge, fliersize, linewidth)
   2235     if ax is None:
   2236         ax = plt.gca()

File ~\anaconda3\Lib\site-packages\seaborn\categorical.py:785, in _BoxPlotter.__init__(self, x, y, hue, data, order, hue_order, orient, color, palette, saturation, width, dodge, fliersize, linewidth)
    781 def __init__(self, x, y, hue, data, order, hue_order,
    782              orient, color, palette, saturation,
    783              width, dodge, fliersize, linewidth):
--> 785     self.establish_variables(x, y, hue, data, orient, order, hue_order)
    786     self.establish_colors(color, palette, saturation)
    788     self.dodge = dodge

File ~\anaconda3\Lib\site-packages\seaborn\categorical.py:486, in _CategoricalPlotter.establish_variables(self, x, y, hue, data, orient, order, hue_order, units)
    484 if hasattr(data, "shape"):
    485     if len(data.shape) == 1:
--> 486         if np.isscalar(data[0]):
    487             plot_data = [data]
    488         else:

File ~\anaconda3\Lib\site-packages\pandas\core\series.py:1007, in Series.__getitem__(self, key)
   1004     return self._values[key]
   1006 elif key_is_scalar:
-> 1007     return self._get_value(key)
   1009 if is_hashable(key):
   1010     # Otherwise index.get_value will raise InvalidIndexError
   1011     try:
   1012         # For labels that don't resolve as scalars like tuples and frozensets

File ~\anaconda3\Lib\site-packages\pandas\core\series.py:1116, in Series._get_value(self, label, takeable)
   1113     return self._values[label]
   1115 # Similar to Index.get_value, but we do not fall back to positional
-> 1116 loc = self.index.get_loc(label)
   1118 if is_integer(loc):
   1119     return self._values[loc]

File ~\anaconda3\Lib\site-packages\pandas\core\indexes\base.py:3655, in Index.get_loc(self, key)
   3653     return self._engine.get_loc(casted_key)
   3654 except KeyError as err:
-> 3655     raise KeyError(key) from err
   3656 except TypeError:
   3657     # If we have a listlike key, _check_indexing_error will raise
   3658     #  InvalidIndexError. Otherwise we fall through and re-raise
   3659     #  the TypeError.
   3660     self._check_indexing_error(key)

KeyError: 0
1 resposta
solução!

Oi, Alexandre! Tudo certo?

Para utilizar o boxplot da biblioteca Seaborn neste contexto, precisamos indicar o eixo onde serão adicionados os dados de medias_por_filme (devido a uma atualização). Podemos escolher o eixo Y (eixo vertical) para organizar a distribuição dos dados, chegando neste código:

sns.boxplot(y=medias_por_filmes)

Caso queira se aprofundar na sintaxe do boxplot, recomendo a leitura do material abaixo:

A página está em inglês, no entanto, caso não tenha familiaridade com o idioma, você pode clicar com o botão direito do mouse e escolher a opção de traduzir para o português!

Espero que dê tudo certo, Alexandre! Qualquer dúvida, estarei por aqui.

Um abraço.

Caso este post tenha lhe ajudado, por favor, marcar como solucionado ✓. Bons Estudos!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software