ValueError: Could not interpret input '0' with Seaborn?

ValueError: Could not interpret input '0' with Seaborn?

我正在使用 Kepler exoplanet dataset.

加载后,运行 对其进行简单的 transpose(),为了将行作为列,我尝试了一个 seaborn 箱线图,如下所示:

sns.boxplot(data=df_train.tail(3197), x='0')

这个returns:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [45], in <module>
----> 1 sns.boxplot(data=df_train.tail(3197), x='0')

File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\seaborn\_decorators.py:46, in _deprecate_positional_args.<locals>.inner_f(*args, **kwargs)
     36     warnings.warn(
     37         "Pass the following variable{} as {}keyword arg{}: {}. "
     38         "From version 0.12, the only valid positional argument "
   (...)
     43         FutureWarning
     44     )
     45 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46 return f(**kwargs)

File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\seaborn\categorical.py:2243, in boxplot(x, y, hue, data, order, hue_order, orient, color, palette, saturation, width, dodge, fliersize, linewidth, whis, ax, **kwargs)
   2231 @_deprecate_positional_args
   2232 def boxplot(
   2233     *,
   (...)
   2240     **kwargs
   2241 ):
-> 2243     plotter = _BoxPlotter(x, y, hue, data, order, hue_order,
   2244                           orient, color, palette, saturation,
   2245                           width, dodge, fliersize, linewidth)
   2247     if ax is None:
   2248         ax = plt.gca()

File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\seaborn\categorical.py:406, in _BoxPlotter.__init__(self, x, y, hue, data, order, hue_order, orient, color, palette, saturation, width, dodge, fliersize, linewidth)
    402 def __init__(self, x, y, hue, data, order, hue_order,
    403              orient, color, palette, saturation,
    404              width, dodge, fliersize, linewidth):
--> 406     self.establish_variables(x, y, hue, data, orient, order, hue_order)
    407     self.establish_colors(color, palette, saturation)
    409     self.dodge = dodge

File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\seaborn\categorical.py:153, in _CategoricalPlotter.establish_variables(self, x, y, hue, data, orient, order, hue_order, units)
    151     if isinstance(var, str):
    152         err = "Could not interpret input '{}'".format(var)
--> 153         raise ValueError(err)
    155 # Figure out the plotting orientation
    156 orient = infer_orient(
    157     x, y, orient, require_numeric=self.require_numeric
    158 )

ValueError: Could not interpret input '0'

我也试过这个:

sns.boxplot(df_train.tail(3197)['0'])

却得到了 KeyError: '0'。我究竟做错了什么?据我所知,我正在做与 Seaborn 官方网站上的第一个示例完全相同的事情,here.

一个可行的例子:

filepath = "exoTrain.csv"
    
df_train = pd.read_csv(filepath)
df_train = df_train.transpose()

print(df_train.columns)

这输出:RangeIndex(start=0, stop=5087, step=1) 这告诉我们索引实际上是整数,而不是字符串,例如“0”、“1”、“2”[​​=13=]

将代码更改为 sns.boxplot(data=df_train.tail(3197), x=0) 可解决问题。