ValueError:无法解释 seaborn 中的输入

pat*_*lia 0 python boxplot pandas seaborn

我有这个数据框:

df = {'DATE':  [43390, 43599, 43605, 43329, 43440],
      'STORE': [1, 1, 1, 2, 2],
      'LYLTY_CARD_NBR': [1000, 1307, 1343, 2373, 2426],
      'TXN_ID': [1, 348, 383, 974, 1038],
      'PROD_QTY': [2, 3, 2, 5, 3],
      'TOT_SALES': [6.0, 6.3, 2.9, 15.0, 13.8]}

transac = pd.DataFrame(df)

    DATE    STORE   LYLTY_CARD_NBR  TXN_ID  PROD_QTY    TOT_SALES
0   43390   1         1000             1           2    6.0
1   43599   1         1307             348         3    6.3
2   43605   1         1343             383         2    2.9
3   43329   2         2373             974         5    15.0
4   43440   2         2426             1038        3    13.8
Run Code Online (Sandbox Code Playgroud)

当我尝试从 TOT_SALES 列的 seaborn 绘制箱线图时,出现此错误。列的类型是 float64。我用 matplotlib 尝试了同样的方法,但它对 seaborn 不起作用:

sns.boxplot(y='TOT_SALES', df=transac)

ValueError                                Traceback (most recent call last)
<ipython-input-16-29f35d075db2> in <module>
----> 1 sns.boxplot(y='TOT_SALES', df=transac)

~\Anaconda3\lib\site-packages\seaborn\categorical.py in boxplot(x, y, hue, data, order, hue_order, orient, color, palette, saturation, width, dodge, fliersize, linewidth, whis, ax, **kwargs)
   2239     plotter = _BoxPlotter(x, y, hue, data, order, hue_order,
   2240                           orient, color, palette, saturation,
-> 2241                           width, dodge, fliersize, linewidth)
   2242 
   2243     if ax is None:

~\Anaconda3\lib\site-packages\seaborn\categorical.py in __init__(self, x, y, hue, data, order, hue_order, orient, color, palette, saturation, width, dodge, fliersize, linewidth)
    441                  width, dodge, fliersize, linewidth):
    442 
--> 443         self.establish_variables(x, y, hue, data, orient, order, hue_order)
    444         self.establish_colors(color, palette, saturation)
    445 

~\Anaconda3\lib\site-packages\seaborn\categorical.py in establish_variables(self, x, y, hue, data, orient, order, hue_order, units)
    150                 if isinstance(var, str):
    151                     err = "Could not interpret input '{}'".format(var)
--> 152                     raise ValueError(err)
    153 
    154             # Figure out the plotting orientation

ValueError: Could not interpret input 'TOT_SALES'
Run Code Online (Sandbox Code Playgroud)

问题出在哪儿?

Val*_*_Bo 6

您的错误来源是您将transac作为df 参数传递,而seaborn未使用该参数。将其作为数据传递:

sns.boxplot(y='TOT_SALES', data=transac);
Run Code Online (Sandbox Code Playgroud)