这是我尝试代码的小型 10 条记录数据集 这就是它应该看起来的样子(丢失的数据包属性):
这是我编写的出现异常的代码:
for lost_p, plr in zip(df['LOST PACKETS'], df['PACKET LOSS RATIO']):
if (lost_p == 0):
df['LOST PACKETS'] = df['LOST PACKETS'].replace(lost_p, 0)
elif (lost_p > 0 and plr > 0.07):
df['LOST PACKETS'] = df['LOST PACKETS'].replace(lost_p, 1)
elif (lost_p > 0 and plr < 0.07):
df['LOST PACKETS'] = df['LOST PACKETS'].replace(lost_p, 0)
else:
df['LOST PACKETS'] = df['LOST PACKETS'].replace(lost_p, 1)
df
这是我一直在尝试处理的数据集: 这是代码的结果(很明显,它在应该显示 1 的地方显示了 0):
我可以补充一点,当执行
df['LOST PACKETS'].value_counts()
时,它显示:
0 14760
Name: LOST PACKETS, dtype: int64
因为你的条件逻辑是错误的。每次都会覆盖 LOST PACKETS 数据。也许,最后一个条件块等于替换 0 。
解决方案: 您需要选择索引替换;例如,对于第二个 if df[(df["LOST PACKETS"] > 0 ) and (df[df['PACKET LOSS RATIO']] > 0.07)].index 然后更改此索引丢失的数据包值。