特殊过滤器pandas数据帧

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

我有一个数据帧:

   BPR_free_speed  BPR_speed  Volume  time_normalised  free_capacity  
0           17.88  15.913662     580         1.593750          475.0  
1           17.88  15.865198     588         2.041667          475.0  
2           17.88  16.511613     475         0.666667          475.0  
3           17.88  16.882837     401         1.091458          467.0  
4           99999  16.703004     438         1.479167          467.0  
5           17.88  16.553928     467         0.960417          467.0  

如何在特殊条件下获得系列赛?我想找到异常值并将它们放在df["has_outliers"]系列中,就像任何一行中的行值大于550,然后是True,否则为False。

此数据框的输出应该是

     has_outliers
0           True
1           True
2           False 
3           False 
4           True 
5           False 

我认为即使使用numpy也可以做到,但怎么做呢?

python pandas numpy dataframe series
1个回答
4
投票

比较DataFrame.gtDataFrame.any检查每行至少一个True:

df["has_outliers"] = df.gt(500).any(axis=1)

或者计算Trues并转换为整数:

df["has_outliers"] = df.gt(500).sum(axis=1).astype(bool)
© www.soinside.com 2019 - 2024. All rights reserved.