Pandas boxplot:IndexError:列表索引超出范围

时间:2017-07-29 22:45:39

标签: python pandas dataframe boxplot

我有一个大小为df的数据框2x2。当我致电df.boxplot()时,收到IndexError: list index out of range错误消息:

Traceback (most recent call last):
  File "my_code.py", line 155, in <module>
    main()
  File "my_code.py", line 135, in main
    df.boxplot()
  File "/server/software/rhel7/python27_pandas-0.19.2-mkl/lib/python2.7/site-packages/pandas/core/frame.py", line 5749, in boxplot
    return_type=return_type, **kwds)
  File "/server/software/rhel7/python27_pandas-0.19.2-mkl/lib/python2.7/site-packages/pandas/tools/plotting.py", line 2797, in boxplot
    result = plot_group(columns, data.values.T, ax)
  File "/server/software/rhel7/python27_pandas-0.19.2-mkl/lib/python2.7/site-packages/pandas/tools/plotting.py", line 2751, in plot_group
    bp = ax.boxplot(values, **kwds)
  File "/server/software/rhel7/python27_matplotlib-1.5.1-mkl/lib/python2.7/site-packages/matplotlib/__init__.py", line 1812, in inner
    return func(ax, *args, **kwargs)
  File "/server/software/rhel7/python27_matplotlib-1.5.1-mkl/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 3212, in boxplot
    labels=labels)
  File "/server/software/rhel7/python27_matplotlib-1.5.1-mkl/lib/python2.7/site-packages/matplotlib/cbook.py", line 1980, in boxplot_stats
    X = _reshape_2D(X)
  File "/server/software/rhel7/python27_matplotlib-1.5.1-mkl/lib/python2.7/site-packages/matplotlib/cbook.py", line 2245, in _reshape_2D
    if not hasattr(X[0], '__len__'):
IndexError: list index out of range

有趣的是,如果我df.iloc[1,:] = [200, 210],则错误消失。但是,运行df.iloc[1,0] = 200; df.iloc[1,1] = 210并不能解决错误。问题是什么?

print(df)

      C_5  C_10
Date
0     100  150
1     200  210

print(df)df.iloc[1,:] = [200, 210]之后或df.iloc[1,0] = 200; df.iloc[1,1] = 210之后(预期的)看起来相同。

1 个答案:

答案 0 :(得分:0)

查看print('df.dtypes: \n{0}'.format(df.dtypes)),问题是dtypesobject

df.dtypes: 
C_5     float64
C_10    object

应该是:

df.dtypes: 
C_5     float64
C_10    float64

否则您将收到非常明确的错误消息IndexError: list index out of range