在系列中擁有資料并嘗試計算某些值,但在嘗試繪制它們時它不起作用。
# import pandas as pd
import pandas as pd
# import numpy as np
import numpy as np
# simple array
data = np.array([0,0,1,1,1])
ser = pd.Series(data)
print('The mean is ' str(ser.mean()))
# find the variance
print('The variance is ' str(ser.var()))
ser.mean().plot.bar(rot=0) # Attempt to use barplot
ser.var().plot.bar(rot=0) # Attempt to use barplot
以上給出以下錯誤:
The mean is 0.6
The variance is 0.30000000000000004
AttributeError: 'float' object has no attribute 'plot'
在一個圖中顯示這兩個值也很酷。
uj5u.com熱心網友回復:
您嘗試使用的功能僅適用于 pandas DataFrame/Series,但您不能使用它來繪制“float”、“int”值。
對于這種情況,將均值和方差值存盤到資料幀/系列中,如下所示
d = [ser.mean(),ser.var()]
df = pd.Series(d) #pd.DataFrame(d) also works
ax = df.plot.bar(rot=1)
ax.set_xticklabels(['Mean','Variance'])
或者您可以使用其他軟體包繪制條形圖。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/441707.html
標籤:Python 熊猫 matplotlib
上一篇:使用字典映射多個列和值
下一篇:將值保存在單獨的列中
