python - 在运行 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()
解决方案
首先,让我们这样做;
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
推荐阅读
- ios - `UIView.layoutSublayers(of:)` 中的 `layer` 参数是否总是视图的图层?
- r - 根据 data.table R 中的两列创建分类变量
- python - 如何解析压缩的 XML 响应?
- python - 如何用最后更新的值减去熊猫数据帧的连续行
- web-scraping - 是否可以链接到社交资料以使用户无法获得 LinkedIn 的“authwall”?
- amazon-web-services - 带有 S3 存储桶的 CNAME
- javascript - 无法阻止使用带有 runAt document_start 的扩展内容脚本绘制 DOM 元素
- node.js - 如何在没有 babel 的情况下修复“语法错误 - 当前未启用对实验性语法 'decorators-legacy' 的支持”
- javascript - 在 JS 中组织嵌套对象数据?
- python - 无法使用 flask_socketio 向客户端发送消息