Pandas boxplot: IndexError: list index out of range
Pandas boxplot: IndexError: list index out of range
我有一个数据框 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
之后看起来一样(这是预期的)。
查看 print('df.dtypes: \n{0}'.format(df.dtypes))
,问题是 dtypes
是 object
df.dtypes:
C_5 float64
C_10 object
而应该是:
df.dtypes:
C_5 float64
C_10 float64
否则您将收到非常明确的错误消息 IndexError: list index out of range
。
我有一个数据框 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
之后看起来一样(这是预期的)。
查看 print('df.dtypes: \n{0}'.format(df.dtypes))
,问题是 dtypes
是 object
df.dtypes:
C_5 float64
C_10 object
而应该是:
df.dtypes:
C_5 float64
C_10 float64
否则您将收到非常明确的错误消息 IndexError: list index out of range
。