Python Forum
unsupported operand type(s) for /: 'str' and 'int' Error for boxplot
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unsupported operand type(s) for /: 'str' and 'int' Error for boxplot
#1
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
Reply
#2
I have edited the qns.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Wrong type error rowan_bradley 6 1,143 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Type Error: Unsupported Operand jhancock 2 1,067 Jul-22-2023, 11:33 PM
Last Post: jhancock
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,207 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,096 May-07-2022, 08:40 AM
Last Post: ibreeden
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 4 32,705 May-07-2022, 08:07 AM
Last Post: menator01
  Python Anytree - Is not of type 'NodeMixin' error georgebijum 3 2,026 May-05-2022, 01:43 PM
Last Post: Gribouillis
  Incorrect Type Error milkycow 4 2,828 Jun-25-2021, 06:04 AM
Last Post: milkycow
Star Type Error: 'in' object is not callable nman52 3 3,328 May-01-2021, 11:03 PM
Last Post: nman52
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 3,023 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  Type Error in Python MarcusB 3 2,527 Mar-30-2021, 06:34 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020