Python Dataframe:基于前面的行值选择行
我有以下Python dataframe对象df:Python Dataframe:基于前面的行值选择行,python,pandas,dataframe,Python,Pandas,Dataframe,我有以下Python dataframe对象df: 1_count 136088194_count 136088202_count Label 1 0.0 0.0 0.0 False 2 0.0 0.0 0.0 False 3 0.0 0.0 0.0 True 4 0.0 0.0
1_count 136088194_count 136088202_count Label
1 0.0 0.0 0.0 False
2 0.0 0.0 0.0 False
3 0.0 0.0 0.0 True
4 0.0 0.0 0.0 False
5 0.0 0.0 0.0 False
6 0.0 0.0 0.0 True
7 6.0 0.0 0.0 False
8 0.0 0.0 0.0 False
9 0.0 0.0 0.0 False
我想创建一个新的Dataframe,它包含出现在最后一列中“Label”值“True”之前的所有行
在本例中,它将是第2行和第5行
结果应该如下所示:
1_count 136088194_count 136088202_count Label
2 0.0 0.0 0.0 False
5 0.0 0.0 0.0 False
我知道我可以通过以下方式访问第3行和第6行:
df = df.loc[df['Label']==True]
但是如何将数据帧移到前面的行呢?一种方法是使用
shift
df = df.loc[df.Label.shift(-1)==True]
print(df)
# Output
1_count 136088194_count 136088202_count Label
2 0.0 0.0 0.0 False
5 0.0 0.0 0.0 False