简体   繁体   中英

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.

One of the columns, named "CBSM", contains boolean values. I need to delete all rows from the dataframe where the value of the CBSM column = "Y".

I see that there is a method called 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. I have a feeling the drop() method is not the right way to do what I want.

Please advise, thanks.

This method is called boolean indexing .

You can try loc with 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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