首页 > 解决方案 > 在运行 python 程序时出现 IndexError: index 3 is out of bounds for axis 0 with size 1 in line 20 df3=df3[df3.columns[3]]

问题描述

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style

ds1 = pd.read_csv("latency.csv")
df1 = pd.DataFrame(ds1)
df1 = df1[df1.columns[1]]
latency_list = []
for latency in df1:
    latency_list.append(latency)
s = pd.DataFrame(ds1)
s = s[s.columns[6]]
stamp_list = []
for stamp in s:
    stamp_list.append(s)
ds2 = pd.read_csv("PacketLoss.csv")
packet_loss = (ds2["Count"][0]) - (ds2['Count'][4])

ds3 = pd.read_csv("RTP Packet Data.csv")
df3 = pd.DataFrame(ds3)
df3 = df3[df3.columns[3]]

average_jitter = df3.mean()

codec_delay = 10
for i in latency_list:
    effective_latency = i + 2 * average_jitter + codec_delay
    if effective_latency < 160:

        r = 93.2 - (effective_latency / 40)
    else:
        r = 93.2 - ((effective_latency - 120) / 10)
    r = r - 2.5 * packet_loss
    if r < 0:
        mos = 1.0
    else:

        mos = 1 + 0.035 * r + 0.000007 * r * (r - 60) * (100 - r)

style.use('fivethirtyeight')
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)


def animate(i):
    xs = []
    ys = []
    for j in stamp_list:
        xs.append(float(j))
    ys.append(float(mos))
    ax1.clear()
    ax1.plot(xs, ys)


ticks = [4.4, 4, 3, 2, 1]

labels = ["Best", "High", "Medium", "Low", "Poor"]
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.yticks(ticks, labels)
plt.xlabel("Timestamp")
plt.ylabel("Mean Openion Score")
plt.title("MOS VS TIME")
plt.show()

标签: pythonpandasdataframematplotlib

解决方案


首先,让我们这样做;

df1 = pd.read_csv("latency.csv")

你不需要这样做(冗余);

ds1 = pd.read_csv("latency.csv")
df1 = pd.DataFrame(ds1)

我看到你在这里做什么。从 df3.columns 列表中索引列。但它看起来好像它一定不存在。

df3 = df3[df3.columns[3]]

请记住,索引从 0 开始,然后向上移动。最好改为这样做;

df3 = df3['column name'] # select the values in this specific column

使用列名而不是索引名要好得多。至少在我的经验中。

实际上,您最好让其他一些变量存储该列。喜欢;

average_jitter = df3['column name'].mean()

这样您就不会覆盖 DataFrame 并且可以继续引用它。

你也可以做这样的事情来枚举你的DataFrame,我开始重视它而不是移动一个列表或一个范围。

for c,r in df1.iterrows():
    latency_list.append(r['latency'])
# or whatever the column name is that has latency stored in it

推荐阅读