本文介绍了ValueError:传递的项目数量错误500,放置位置表示1,Python和Pandas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只从.xlsx文件导入两列,我想计算一些数据(均值,偏差,变化百分比),然后我想绘制所有这些内容.第一部分没有给我带来任何问题,但绘图却给了我.

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.

我的代码如下:

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`

我发现问题出在:

plt.plot(x,mlab.normpdf(x,ma,std))

但我无法解决.有什么建议?

but I cannot solve it. Any suggestions?

推荐答案

mastd在您的示例中是pandas.Series个对象.原因是,将np.mean应用于pandas.DataFrame会返回pandas.Series.但是,mlab.normpdf(x,ma,std)期望将float值或numpy数组作为输入.您可以简单地将mastd转换为ma = float(ma)的浮点数.我不建议您使用注释中指出的int(ma),因为这样会减少小数点.

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.

这篇关于ValueError:传递的项目数量错误500,放置位置表示1,Python和Pandas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 02:28