imharindersingh
imharindersingh

Reputation: 196

Plotting histogram of Pandas Dataframe with its mean and standard deviation, getting ValueError

I have a pandas dataframe consisting of daterange as index and one column and 2192 rows. I am able to plot this dataframe on histogram but when I try to include mean and standard deviation of this dataframe on histogram I get a ValueError caused at these lines

#compute daily returns
daily_returns = stats.compute_daily_returns(df_btc)
#plotter.plot_data(daily_returns, title="Daily returns", ylabel="Daily returns")
print daily_returns.head()

daily_returns.replace(to_replace=np.inf, value=np.NaN, inplace=True)
# Plot a histogram
daily_returns.hist(bins=21)
plt.show()

# Get mean as standard deviation
mean = daily_returns.mean()
std = daily_returns.std()

print mean
print std

#the problem comes here  
plt.axvline(mean, color='w', linestyle='dashed', linewidth=2)

enter image description here

The error is in dataextract.py of my repository on github

I have gone through this link too but it didn't solve my problem

Upvotes: 4

Views: 8498

Answers (1)

tmdavison
tmdavison

Reputation: 69076

The problem is that you are trying to use a 1-item Series in axvline instead of a float.

The .mean() of a pandas.DataFrame is a pandas.Series, not a float.

In this case, you can fix it by just taking the value stored in the Series

plt.axvline(mean[0], color='w', linestyle='dashed', linewidth=2)

Upvotes: 7

Related Questions