[英]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?
Very simple question everyone, but nearly impossible to find answers to basic questions in official documentation. 每个人都非常简单的问题,但是几乎不可能在官方文档中找到基本问题的答案。
I have a dataframe object in Pandas that has rows and columns. 我在Pandas中有一个具有行和列的dataframe对象。
One of the columns, named "CBSM", contains boolean values. 名为“ CBSM”的列之一包含布尔值。 I need to delete all rows from the dataframe where the value of the CBSM column = "Y".
我需要从数据框CBSM列的值=“ Y”删除所有行。
I see that there is a method called dataframe.drop()
我看到有一个名为
dataframe.drop()
的方法
Label, Axis, and Level are 3 parameters that the drop()
method takes in. I have no clue what values to provide these parameters to accomplish my need of deleting the rows in the fashion I described above. Label,Axis和Level是
drop()
方法采用的3个参数。我不知道提供这些参数的值是什么,以上述方式完成删除行的需求。 I have a feeling the drop()
method is not the right way to do what I want. 我感觉
drop()
方法不是执行我想要的正确方法。
Please advise, thanks. 请指教,谢谢。
This method is called boolean indexing . 此方法称为布尔索引 。
You can try loc
with str.contains
: 您可以尝试
loc
与str.contains
:
df.loc[~df['CBSM'].str.contains('Y')]
Sample: 样品:
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
Or: 要么:
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.