Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Row from Pandas DataFrame based on cell value

How can i delete a row in a Pandas Dataframe, based on a cell value without giving a specific column name?

For example:

I have this DataFrame and i want to delete all rows where a cell contains the value 'd'.

   A B C D
1  1 2 d 5
2  1 3 4 0
3  d 2 1 2
4  3 2 1 7

So I end up with the DataFrame

  A B C D
2 1 3 4 0
4 3 2 1 7

Is there a way to achive this? My google skills only found solutions where a specific column name is required.

like image 677
user3657850 Avatar asked Sep 11 '25 15:09

user3657850


1 Answers

you can do it this way:

df = df[~df.select_dtypes(['object']).eq('d').any(1)]

Result:

In [23]: df
Out[23]:
   A  B  C  D
2  1  3  4  0
4  3  2  1  7
like image 197
MaxU - stop WAR against UA Avatar answered Sep 13 '25 10:09

MaxU - stop WAR against UA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!