2015-11-12 2 views
5
から

までです.xlsxファイルから2列だけをインポートしていますが、何か(平均、偏差、変化率)を計算したいと思いますこのすべてをプロットするのが好きです。最初の部分は私に問題を与えませんが、プロットはしません。ValueError:アイテム数が間違って500件、プレースメント数が1で、PythonとPandasが

私のコードは次のようになります。

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() 

しかし、私は自分のコードを実行したとき、私はこのエラーを取得:

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))

でも解決できません。助言がありますか?

+1

あなたは 'df'がどのように見えるかを教えてください。例えば'df.head()'の出力です。プロットせずに 'y = mlab.normpdf(x、ma、std)'を計算しようとすることができますか?私はあなたもエラーを起こすと思います。 'print ma'と' print std'の出力を表示できますか?私の賭けは彼らが浮かれているのではなく、パンダシリーズです。 –

+0

@FabianRost、df.headの出力は、()である:VEP 日2005-05-16 4.1729 2005-05-17 4.1700 4.3400 2005-05-18 2005-05-19 4.3600 2005-05-20 4.3700 – DavidV

+0

印刷出力(ma):VEP 7.14688 dtype:float64とprint(std)の出力:VEP 19.335596 dtype:float64 – DavidV

答えて

5

maおよびstdは、例ではpandas.Seriesです。理由は、pandas.DataFrameに適用されたnp.meanpandas.Seriesを返すからです。 しかし、mlab.normpdf(x、ma、std)はfloat値またはnumpy配列を入力として想定しています。 mastdma = float(ma)で単純にフロートに変換できます。 int(ma)をあなたのコメントで指摘したように使用することはお勧めしません。これは小数点以下を切り捨てるためです。

+1

ファビアン、もう一度ありがとう。私はフロート(ma)とフロート(std)を使用し、今のところ動作します。 – DavidV

関連する問題