ValueError:错误的项目数通过 500,放置意味着 1,Python 和 Pandas
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33677780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: Wrong number of items passed 500, placement implies 1, Python and Pandas
提问by DavidV
I'm importing just two columns from .xlsx file and I would like to calculate some stuff (mean, deviation, percent change) and then I would like to plot all this. First part doesn't give me any problems, but plotting does.
我只从 .xlsx 文件中导入两列,我想计算一些东西(平均值、偏差、百分比变化),然后我想绘制所有这些。第一部分没有给我任何问题,但绘图有。
My code looks like this:
我的代码如下所示:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.mlab as mlab
import math
df = pd.read_excel('KDPrviIzbor.xlsx', sheetname='List1', index_col = 0)
ch = df.pct_change(periods=252)
ma = np.mean(ch)*100
std = np.std(ch)*100
x = np.linspace(-100,100,500)
plt.plot(x,mlab.normpdf(x,ma,std))
plt.show()
But when I run my code, I get this error:
但是当我运行我的代码时,我收到了这个错误:
Traceback (most recent call last):
File "C:/Users/David/PythonStuff/normal_distribution.py", line 21, in <module> plt.plot(x,mlab.normpdf(x,ma,std))
File "C:\Python27\lib\site-packages\matplotlib\mlab.py", line 1579, in normpdf return 1./(np.sqrt(2*np.pi)*sigma)*np.exp(-0.5 * (1./sigma*(x - mu))**2)
File "C:\Python27\lib\site-packages\pandas\core\ops.py", line 534, in wrapper dtype=dtype)
File "C:\Python27\lib\site-packages\pandas\core\series.py", line 220, in __init__ data = SingleBlockManager(data, index, fastpath=True)
File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 3383, in __init__ ndim=1, fastpath=True)
File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 2101, in make_block placement=placement)
File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 77, in __init__ len(self.values), len(self.mgr_locs)))
ValueError: Wrong number of items passed 500, placement implies 1`
I figured that the problem is in:
我认为问题出在:
plt.plot(x,mlab.normpdf(x,ma,std))
plt.plot(x,mlab.normpdf(x,ma,std))
but I cannot solve it. Any suggestions?
但我无法解决它。有什么建议?
采纳答案by Fabian Rost
ma
and std
are pandas.Series
objects in your example. The reason is, that np.mean
applied to a pandas.DataFrame
returns a pandas.Series
.
However, mlab.normpdf(x,ma,std) expects float values or numpy arrays as inputs.
You could simply convert ma
and std
to floats by ma = float(ma)
.
I would not suggest to use int(ma)
as you pointed out in your comment, because that would cut away the decimals.
ma
并且std
是pandas.Series
您示例中的对象。原因是,np.mean
应用于 apandas.DataFrame
返回 a pandas.Series
。但是, mlab.normpdf(x,ma,std) 需要浮点值或 numpy 数组作为输入。您可以简单地将ma
和转换std
为浮点数ma = float(ma)
。我不建议int(ma)
像您在评论中指出的那样使用,因为这会减少小数点。