我使用的barchart
功能取自here。
def barchart(x,y,x_title,y_title,numbins=10):
datarange=max(x)-min(x)
bin_width=float(datarange)/numbins
pos=min(x)
bins=[0 for i in range(numbins+1)]
for i in range(numbins):
bins[i]=pos
pos+=bin_width
bins[numbins]=max(x)+1
binsum=[0 for i in range(numbins)]
bincount=[0 for i in range(numbins)]
binaverage=[0 for i in range(numbins)]
for i in range(numbins):
for j in range(len(x)):
if x[j]>=bins[i] and x[j]<bins[i+1]:
bincount[i]+=1
binsum[i]+=y[j]
for i in range(numbins):
binaverage[i]=float(binsum[i])/bincount[i]
barplot(range(numbins),binaverage,x_title,y_title)
我按以下方式运行此函数(df
是一个pandas数据框):
barchart(df.DAY_OF_WEEK, df.WEATHER_DELAY, "WEATHER DELAY", "DAY", len(df.DAY_OF_WEEK.unique()))
第if x[j]>=bins[i] and x[j]<bins[i+1]:
行给出了以下错误消息:
ValueError:系列的真值是不明确的。使用a.empty, a.bool(),a.item(),a.any()或a.all()。