如何在分组数据python中执行for循环

ttp71kqs  于 2021-07-14  发布在  Java
关注(0)|答案(2)|浏览(314)

我一直在尝试不同的方法来探测失败的着陆。在代码中,我使用for循环设置了几个条件,但它并没有给出所需的结果。目标是在索引2和索引4处获得“真”值。
下面您可以找到一个虚拟Dataframe。首先,我创建了新的列来计算当前高度与特定飞机先前高度之间的差异。然后过滤na值和极值。完成后,我创建了一个初始值为“false”的新列。
然后,for循环应该检查每一行,看看高度变化是否大于30英尺,如果是这样的话,应该从那一行开始分配一个“真”直到着陆(高度=0)
然而,当我使用包含不同飞机id的Dataframe时,这个for循环似乎不起作用。

df = pd.DataFrame({
        'aircraft':     ['1', '1', '1', '2','1','1' ,'2','3','2','3', '3'],
        'altitude':     [1000, 900, 1200, 1000, 1400, 0, 890, 1050, 750, 850, 700],
    })

    # Creating two new columns showing the difference in altitude between transmissions
    df["altitude_shift"] = df.groupby(['aircraft'])["altitude"].shift(1)
    df["altitude_difference"] = df["altitude"] - df["altitude_shift"]

    # Replacing the NaN values with 0
    df.replace(np.nan,0)
    df['altitude_difference'] = df['altitude_difference'].fillna(0)

    # Filtering out the negative values, showing only the points where the aircraft climbs during approach
    df["altitude_climb"] = np.where(df["altitude_difference"]<0,
                                             0, df["altitude_difference"])

    # Filtering out the extreme climb values
    df['altitude_climb'] = np.where(df['altitude_climb']>800,
                                              0, df['altitude_climb'])

    # Creating a new column with False as initial value
    df['aborted_landing'] = False

    # Creates True values if the aircraft gains more than 30 ft between alt reports
    # The elif statement checks if the aircraft is on ground
    for indices, row in df.iterrows():
        if df.at[indices, 'altitude_climb'] > 30:
            df.loc[indices:, 'aborted_landing'] = True
        elif df.at[indices, 'altitude'] <= 0:
            df.loc[indices:, 'aborted_landing'] = False
        break

有什么建议吗?

mmvthczy

mmvthczy1#

代码末尾的for循环仅对df的第0行执行条件检查。你的for循环结束时的中断应该在那里吗?

dgjrabp2

dgjrabp22#

我不认为有必要:
创建初始值为false的新列df['aborted\u landing']=false
以及for循环。
就这么做吧 df['aborted_landing]= df['altitude_climb'] >30

相关问题