Skip to content
Advertisement

ValueError: Could not interpret input ‘0’ with Seaborn?

I’m using the Kepler exoplanet dataset.

After loading it, and running a simple transpose() on it, in order to get the rows as columns, I try a seaborn boxplot, as follows: enter image description here

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

This returns:

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

File c:usersmartiappdatalocalprogramspythonpython39libsite-packagesseaborn_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:usersmartiappdatalocalprogramspythonpython39libsite-packagesseaborncategorical.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:usersmartiappdatalocalprogramspythonpython39libsite-packagesseaborncategorical.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:usersmartiappdatalocalprogramspythonpython39libsite-packagesseaborncategorical.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'

I also attempted this:

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

and got a KeyError: '0' instead. What am I doing wrong? As far as I can tell, I’m doing the exact same thing as the first example on the official Seaborn website, here.

Advertisement

Answer

A workable example:

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

print(df_train.columns)

This outputs: RangeIndex(start=0, stop=5087, step=1) which shows us that the indices are actually integers, rather than strings e.g. ‘0’, ‘1’, ‘2’

Changing the code to sns.boxplot(data=df_train.tail(3197), x=0) fixes the issue.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement