奇怪的“单个位置索引器超出范围”错误

时间:2020-09-11 19:37:12

标签: python python-3.x pandas seaborn

在过去的大约一个小时内,我一直在尝试解决此问题,但无济于事。我对这个错误感到困惑,因为据我所知(作为一个新手),它随机停止了工作。现在,我知道那不是事实。因此,我来​​这里是为了了解为什么代码在第二种情况下会产生错误,但是相同的代码(数据库的一部分)在第一种情况下不会产生错误。谢谢您的时间!

首先,我将分享正常运行的信息。

运行代码的数据库:The database of the functioning code:

功能代码:

# 3.6 plot severity vs junction type
indvar = 'JUNCTIONTYPE'
depvar = 'Junction Type'

# prepare the data to be plotted
dflc = (dfm2
    .groupby([indvar, 'SEVERITYCODE'])
    .size()
    .reset_index()
    .replace({'SEVERITYCODE': {1:"prop. damage", 2:"minor injury"}})
    .rename(columns={0:"count"}))

# calculate survival % per town of embarkation    
dflc["percent"] = (dflc
                 .groupby(indvar)
                 .apply(lambda x: x["count"] / x["count"].sum()).values)

# sort the dataframe to match the drawing order    
dflc.sort_values(by=['SEVERITYCODE', indvar], inplace = True)

# plot severity vs lighting conditions
plt.style.use('ggplot')
fig = sb.catplot(
    x="count", y=indvar, hue='SEVERITYCODE',
    kind="bar", data=dflc, height=10, aspect=2, legend_out = False)

for i, bar in enumerate(fig.ax.patches):
   
    height = bar.get_height()
    fig.ax.annotate(
        # reference the pre-calculated row in the dataframe
        f"{dflc.iloc[i, 3] :.0%}",
        xycoords="data",
        xytext=(30, -15),
        fontsize = 18,
        textcoords="offset points",
        xy=(bar.get_width(), bar.get_y()),
        ha='center', va='center')
    
# make space for annonations
plt.margins(x=0.2)

# make final adjustments
plt.title(depvar + ' vs. Number of Collisions by Severity', fontsize = 30)
plt.xlabel('Number of Collisions', fontsize = 24)
plt.ylabel(depvar, fontsize = 24)
plt.tick_params(labelsize=18)
plt.legend(fontsize = 18, title = "Severity", loc = 'lower right', title_fontsize = 18, shadow = True)
plt.show()

代码的输出: The code's output

第二,产生错误的代码信息:

数据库:

enter image description here

代码:

# 3.8 plot severity vs number of people 
indvar = 'PERSONCOUNT'
depvar = 'Number of People'

# prepare the data to be plotted
dflc = (dfm2
    .groupby([indvar, 'SEVERITYCODE'])
    .size()
    .reset_index()
    .replace({'SEVERITYCODE': {1:"prop. damage", 2:"minor injury"}})
    .rename(columns={0:"count"}))

# calculate survival % per town of embarkation    
dflc["percent"] = (dflc
                 .groupby(indvar)
                 .apply(lambda x: x["count"] / x["count"].sum()).values)

# sort the dataframe to match the drawing order    
dflc.sort_values(by=['SEVERITYCODE', indvar], inplace = True)

# plot severity vs lighting conditions
plt.style.use('ggplot')
fig = sb.catplot(
    x="count", y=indvar, hue='SEVERITYCODE',
    kind="bar", data=dflc, height=10, aspect=2, legend_out = False)

for i, bar in enumerate(fig.ax.patches):
   
    height = bar.get_height()
    fig.ax.annotate(
        # reference the pre-calculated row in the dataframe
        f"{dflc.iloc[i, 3] :.0%}",
        xycoords="data",
        xytext=(20, -15),
        fontsize = 18,
        textcoords="offset points",
        xy=(bar.get_width(), bar.get_y()),
        ha='center', va='center')
    
# make space for annonations
plt.margins(x=0.2)

# make final adjustments
plt.title(depvar + ' vs. Number of Collisions by Severity', fontsize = 30)
plt.xlabel('Number of Collisions', fontsize = 24)
plt.ylabel(depvar, fontsize = 24)
plt.tick_params(labelsize=18)
plt.legend(fontsize = 18, title = "Severity", loc = 'lower right', title_fontsize = 18, shadow = True)
plt.show()

输出:

enter image description here

0 个答案:

没有答案