Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If row in Dataframe contains certain string delete

I have to delete values in row of a dataframe if they contain a certain string. Problem is that row is very long and contains text.

Loop does not work and put index in a list and then use .drop on index does also not work.

column1
8
8
8
8 total       <-------- This must be deleted
8
8 
8 
8
8
...

Thanks

like image 502
Patrick Lombardo Avatar asked Oct 19 '25 05:10

Patrick Lombardo


1 Answers

Suppose your dataframe is called df. Then use:

df_filtered = df[~df['column1'].str.contains('total')]

Explanation:

df['column1'].str.contains('total') will give you an array of the length of the dataframe column that is True whereever df['column1'] contains 'total'. With ~ you swap the True and False values of this array. And finally with df_filtered = df[...] you take only the lines, for which 'total' is not included.

like image 143
markuscosinus Avatar answered Oct 20 '25 18:10

markuscosinus



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!