繁体   English   中英

当特定列包含向我发出信号应删除该行的值时,如何删除Pandas数据框中的行?

[英]How do I delete a row in Pandas dataframe when a specific column contains a value that signals to me that the row should be deleted?

每个人都非常简单的问题,但是几乎不可能在官方文档中找到基本问题的答案。

我在Pandas中有一个具有行和列的dataframe对象。

名为“ CBSM”的列之一包含布尔值。 我需要从数据框CBSM列的值=“ Y”删除所有行。

我看到有一个名为dataframe.drop()的方法

Label,Axis和Level是drop()方法采用的3个参数。我不知道提供这些参数的值是什么,以上述方式完成删除行的需求。 我感觉drop()方法不是执行我想要的正确方法。

请指教,谢谢。

此方法称为布尔索引

您可以尝试locstr.contains

df.loc[~df['CBSM'].str.contains('Y')] 

样品:

print df
   A CBSM  L
0  1    Y  4
1  1    N  6
2  2    N  3

print df['CBSM'].str.contains('Y')
0     True
1    False
2    False
Name: CBSM, dtype: bool

#inverted boolean serie
print ~df['CBSM'].str.contains('Y')
0    False
1     True
2     True
Name: CBSM, dtype: bool

print df.loc[~df['CBSM'].str.contains('Y')] 
   A CBSM  L
1  1    N  6
2  2    N  3

要么:

print df.loc[~(df['CBSM'] == 'Y')] 
   A CBSM  L
1  1    N  6
2  2    N  3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM