Paw*_*rBI 2 matplotlib pandas seaborn
我正在尝试创建一个函数,该函数将遍历数据框中的数字特征列表,以在其旁边显示直方图和汇总统计信息。我正在使用plt.figtext()
显示统计信息,但出现错误
num_features=[n1,n2,n3]
for i in num_features:
fig, ax = plt.subplots()
plt.hist(df[i])
plt.figtext(1,0.5,df[i].describe() )
ax.set_title(i)
plt.show()
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我收到一条错误/警告消息:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
如果使用df[n].mean()
而不是它工作正常describe()
我究竟做错了什么?有没有更好的方法来打印一个图并在它旁边显示一些统计数据?
您可以通过使用以下格式将返回的数据帧格式化describe()
为字符串来“简化”您的代码to_string()
:
df = pd.DataFrame(np.random.normal(size=(2000,)))
fig, ax = plt.subplots()
ax.hist(df[0])
plt.figtext(0.1,0.5, df.describe().to_string())
plt.figtext(0.75,0.5, df.describe().loc[['mean','std']].to_string())
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2498 次 |
最近记录: |