Python Forum
unsupported operand type(s) for /: 'str' and 'int' Error for boxplot - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: unsupported operand type(s) for /: 'str' and 'int' Error for boxplot (/thread-32432.html)



unsupported operand type(s) for /: 'str' and 'int' Error for boxplot - soft - Feb-09-2021

I can't plot the boxplot for Gender and Age. Anybody know why?
df.boxplot(by ='Gender', column =['Age']) 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-6c670e351f01> in <module>
      1 #boxplot = df.boxplot(column=['Age'])
      2 
----> 3 boxplot = df.boxplot(by ='Gender', column =['Age'])
      4 boxplot

~\anaconda3\lib\site-packages\pandas\plotting\_core.py in boxplot_frame(self, column, by, ax, fontsize, rot, grid, figsize, layout, return_type, backend, **kwargs)
    434 ):
    435     plot_backend = _get_plot_backend(backend)
--> 436     return plot_backend.boxplot_frame(
    437         self,
    438         column=column,

~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\boxplot.py in boxplot_frame(self, column, by, ax, fontsize, rot, grid, figsize, layout, return_type, **kwds)
    362     import matplotlib.pyplot as plt
    363 
--> 364     ax = boxplot(
    365         self,
    366         column=column,

~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\boxplot.py in boxplot(data, column, by, ax, fontsize, rot, grid, figsize, layout, return_type, **kwds)
    314         # Prefer array return type for 2-D plots to match the subplot layout
    315         # https://github.com/pandas-dev/pandas/pull/12216#issuecomment-241175580
--> 316         result = _grouped_plot_by_column(
    317             plot_group,
    318             data,

~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\boxplot.py in _grouped_plot_by_column(plotf, data, columns, by, numeric_only, grid, figsize, ax, layout, return_type, **kwargs)
    204         gp_col = grouped[col]
    205         keys, values = zip(*gp_col)
--> 206         re_plotf = plotf(keys, values, ax, **kwargs)
    207         ax.set_title(col)
    208         ax.set_xlabel(pprint_thing(by))

~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\boxplot.py in plot_group(keys, values, ax)
    285         keys = [pprint_thing(x) for x in keys]
    286         values = [np.asarray(remove_na_arraylike(v)) for v in values]
--> 287         bp = ax.boxplot(values, **kwds)
    288         if fontsize is not None:
    289             ax.tick_params(axis="both", labelsize=fontsize)

~\anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py in wrapper(*args, **kwargs)
    294                 f"for the old name will be dropped %(removal)s.")
    295             kwargs[new] = kwargs.pop(old)
--> 296         return func(*args, **kwargs)
    297 
    298     # wrapper() must keep the same documented signature as func(): if we

~\anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1563     def inner(ax, *args, data=None, **kwargs):
   1564         if data is None:
-> 1565             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1566 
   1567         bound = new_sig.bind(ax, *args, **kwargs)

~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in boxplot(self, x, notch, sym, vert, whis, positions, widths, patch_artist, bootstrap, usermedians, conf_intervals, meanline, showmeans, showcaps, showbox, showfliers, boxprops, labels, flierprops, medianprops, meanprops, capprops, whiskerprops, manage_ticks, autorange, zorder)
   3625             bootstrap = rcParams['boxplot.bootstrap']
   3626 
-> 3627         bxpstats = cbook.boxplot_stats(x, whis=whis, bootstrap=bootstrap,
   3628                                        labels=labels, autorange=autorange)
   3629         if notch is None:

~\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in boxplot_stats(X, whis, bootstrap, labels, autorange)
   1203 
   1204         # arithmetic mean
-> 1205         stats['mean'] = np.mean(x)
   1206 
   1207         # medians and quartiles

<__array_function__ internals> in mean(*args, **kwargs)

~\anaconda3\lib\site-packages\numpy\core\fromnumeric.py in mean(a, axis, dtype, out, keepdims)
   3332             return mean(axis=axis, dtype=dtype, out=out, **kwargs)
   3333 
-> 3334     return _methods._mean(a, axis=axis, dtype=dtype,
   3335                           out=out, **kwargs)
   3336 

~\anaconda3\lib\site-packages\numpy\core\_methods.py in _mean(a, axis, dtype, out, keepdims)
    161             ret = ret.dtype.type(ret / rcount)
    162     else:
--> 163         ret = ret / rcount
    164 
    165     return ret

TypeError: unsupported operand type(s) for /: 'str' and 'int'
Sample of dataframe
	Product	Branch	Age	Gender	Education	MaritalStatus	Usage	Fitness	Income	Miles
0	M1	West	18	Male	14	Single	na	4	29562	112
1	M1	East	19	Male	15	Single	2	3	31836	75
2	M1	West	19	Female	14	Partnered	4	3	30699	66
3	M1	South	na	na	na	na	na	na	na	na
4	M1	West	19	Male	12	Single	na	3	32973	85



RE: Plotting bar chart with multiple conditions - soft - Feb-09-2021

I have edited the qns.