Sto importando solo due colonne dal file .xlsx e vorrei calcolare alcune cose (media, deviazione, variazione percentuale) e poi vorrei piace tracciare tutto questo. La prima parte non mi dà problemi, ma la trama lo fa.ValueError: numero errato di elementi passati 500, il posizionamento implica 1, Python e Pandas
Il mio codice è simile al seguente:
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()
Ma quando eseguo il mio codice, ottengo questo errore:
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`
ho pensato che il problema è in:
plt.plot(x,mlab.normpdf(x,ma,std))
ma non riesco a risolverlo. Eventuali suggerimenti?
potresti mostrare come appare 'df'? per esempio. l'output di 'df.head()'. e puoi provare a calcolare 'y = mlab.normpdf (x, ma, std)' senza tracciare? Suppongo che avrai anche un errore allora. E puoi mostrare l'output di 'print ma' e' print std'? la mia scommessa è che non sono float ma una serie di panda. –
@FabianRost, uscita di df.head() è: VEP Data 2005-05-16 4,1729 2005-05-17 4,1700 2005-05-18 4,3400 2005-05-19 4,3600 2005-05-20 4.3700 – DavidV
Output di stampa (ma): VEP 7.14688 dtype: float64 e output di stampa (std): VEP 19.335596 dtype: float64 – DavidV