为什么 for 循环对于 Google colab 中的较大数据集无法正常工作?

问题描述 投票:0回答:1

这是我尝试代码的小型 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
python machine-learning dataset google-colaboratory
1个回答
0
投票

因为你的条件逻辑是错误的。每次都会覆盖 LOST PACKETS 数据。也许,最后一个条件块等于替换 0 。

解决方案: 您需要选择索引替换;例如,对于第二个 if df[(df["LOST PACKETS"] > 0 ) and (df[df['PACKET LOSS RATIO']] > 0.07)].index 然后更改此索引丢失的数据包值。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.