在数据帧列中查找最后一个匹配值

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

我有一个带有“状态”列的数据框,其值: 真的, 错误的, 弗拉瑟, 真的, 正确

我想找到 False 的最后一个位置并获取仅包含最后两个值的数据帧。

python pandas dataframe filter find
1个回答
0
投票
import pandas as pd

# dataframe as you described
df = pd.DataFrame({
    'status' : [True, False, False, True, True]
})

# get the row number of the last False (credit to mozway for index idea)
last_false = df.index[~df['status']].max()

# get every row after the last false
df.loc[last_false+1:]

# or just getting the last two rows
df.tail(2)
© www.soinside.com 2019 - 2024. All rights reserved.