【发布时间】:2021-12-20 01:00:36
【问题描述】:
import matplotlib.pyplot as plt
loans.boxplot(columns="RevolvingUtilizationOfUnsecuredLines")
在运行上面我得到以下错误
TypeError Traceback (most recent call last)
<ipython-input-20-3237ef4a136d> in <module>
----> 1 loans.boxplot(columns="RevolvingUtilizationOfUnsecuredLines")
~\anaconda3\lib\site-packages\pandas\plotting\_core.py in boxplot_frame(self, column, by, ax, fontsize, rot, grid, figsize, layout, return_type, backend, **kwargs)
492 ):
493 plot_backend = _get_plot_backend(backend)
--> 494 return plot_backend.boxplot_frame(
495 self,
496 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)
383 import matplotlib.pyplot as plt
384
--> 385 ax = boxplot(
386 self,
387 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)
362 data = data[columns]
363
--> 364 result = plot_group(columns, data.values.T, ax)
365 ax.grid(grid)
366
~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\boxplot.py in plot_group(keys, values, ax)
301 keys = [pprint_thing(x) for x in keys]
302 values = [np.asarray(remove_na_arraylike(v), dtype=object) for v in values]
--> 303 bp = ax.boxplot(values, **kwds)
304 if fontsize is not None:
305 ax.tick_params(axis="both", labelsize=fontsize)
~\anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
1445 def inner(ax, *args, data=None, **kwargs):
1446 if data is None:
-> 1447 return func(ax, *map(sanitize_sequence, args), **kwargs)
1448
1449 bound = new_sig.bind(ax, *args, **kwargs)
TypeError: boxplot() got an unexpected keyword argument 'columns'
更新
loans.boxplot(column="RevolvingUtilizationOfUnsecuredLines")
在运行上面我得到以下错误
KeyError Traceback (most recent call last) in ----> 1 loans.boxplot(column="RevolvingUtilizationOfUnsecuredLines")
~\anaconda3\lib\site-packages\pandas\plotting_core.py in boxplot_frame(self, column, by, ax, fontsize, rot, grid, figsize, layout, return_type, backend, **kwargs) 492 ): 493 plot_backend = _get_plot_backend(backend) --> 494 return plot_backend.boxplot_frame( 495 self, 496 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) 383 import matplotlib.pyplot as plt 384 --> 385 ax = boxplot( 386 self, 387 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) 360 columns = data.columns 361 else: --> 362 data = data[columns] 363 364 result = plot_group(columns, data.values.T, ax)
~\anaconda3\lib\site-packages\pandas\core\frame.py in getitem(self, key) 3028 if is_iterator(key): 3029 key = list(key) -> 3030 indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1] 3031 3032 # take() does not accept boolean indexers
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis, raise_missing) 1264 keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr) 1265 -> 1266 self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing) 1267 return keyarr, indexer 1268
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing) 1306 if missing == len(indexer): 1307 axis_name = self.obj._get_axis_name(axis) -> 1308 raise KeyError(f"None of [{key}] are in the [{axis_name}]") 1309 1310 ax = self.obj._get_axis(axis)
KeyError: "None of [Index(['RevolvingUtilizationOfUnsecuredLines'], dtype='object')] are in the [columns]"
Sr_No,SeriousDlqin2yrs,RevolvingUtilizationOfUnsecuredLines,age,NumberOfTime30-59DaysPastDueNotWorse,DebtRatio,MonthlyIncome,NumberOfOpenCreditLinesAndLoans,NumberOfTimes90DaysLate,NumberRealEstateLoansOrLines,NumberOfTime60-89DaysPastDueNotWorse,NumberOfDependents
1,1,0.766126609,45,2,0.802982129,9120.0,13,0,6,0,2.0
2,0,0.957151019,40,0,0.121876201,2600.0,4,0,0,0,1.0
3,0,0.65818014,38,1,0.085113375,3042.0,2,1,0,0,0.0
4,0,0.233809776,30,0,0.036049682,3300.0,5,0,0,0,0.0
5,0,0.9072394,49,1,0.024925695,63588.0,7,0,1,0,0.0
6,0,0.213178682,74,0,0.375606969,3500.0,3,0,1,0,1.0
7,0,0.305682465,57,0,5710.0,,8,0,3,0,0.0
8,0,0.754463648,39,0,0.209940017,3500.0,8,0,0,0,0.0
9,0,0.116950644,27,0,46.0,,2,0,0,0,
10,0,0.189169052,57,0,0.606290901,23684.0,9,0,4,0,2.0
11,0,0.644225962,30,0,0.30947621,2500.0,5,0,0,0,0.0
12,0,0.01879812,51,0,0.53152876,6501.0,7,0,2,0,2.0
13,0,0.010351857,46,0,0.298354075,12454.0,13,0,2,0,2.0
14,1,0.964672555,40,3,0.382964747,13700.0,9,3,1,1,2.0
15,0,0.019656581,76,0,477.0,0.0,6,0,1,0,0.0
【问题讨论】:
-
这是由拼写错误(例如
column=,而不是columns=)引起的,应该在评论中指出,并标记为由拼写错误引起。不要为拼写错误引起的问题添加答案。 SO 不能替代阅读文档pandas.DataFrame.boxplot: column: str or list of str, optional -
鉴于示例
csv,该问题不可重现:请参阅Code and Plot -
To OP 已经提供了足够的信息,但是在解决了第一个问题的错字后,第二个问题无法重现,如我之前的评论所示。我不建议重新提出问题。
-
使用
df = pd.read_csv('cs-training.csv')读取数据,您可以尝试使用.iloc指定逐列索引:ax = df.iloc[:, [2]].boxplot(); ax.set_yscale('log')。但是,ax = df.boxplot(column='RevolvingUtilizationOfUnsecuredLines'); ax.set_yscale('log')应该可以正常工作。 -
好的,谢谢特伦顿
标签: python pandas matplotlib jupyter-notebook boxplot