2

I want to delete rows when a condition when is met: I need to drop any value that is less than zero in specific column.

For instance,I import a csv file:

import pandas as pd
df=pd.read_csv('Surface_Time_PS.csv') 
df.drop_duplicates(subset='DEPTH',keep='first',inplace=True) #DROP ANY DUPLICATION IN DEPTH 

df['Difference1']=df['DEPTH(ft)'].diff()
df['Difference1']

for x in "df.Difference1":
    if [df['Difference1'].diff() < 0]:
        df.drop(df[df['Difference1'].diff() < 0].index, inplace=False)           #FUNCTION WHERE IS THE DIFF IN DEPTH IS MUST BE POSITIVE OTHER WISE DROP THE row
    else:
        df

after running this code, I get rid of most of the row with negative value in Difference1 dropped. But still I have few negative value.

I want the code to repeat the process until the column has only positive values

Moh
  • 31
  • 5

1 Answers1

2

Remove for loop statement and put this line below:

df.drop(df[df['Difference1'] < 0].index, inplace=True)
Kafels
  • 3,864
  • 1
  • 15
  • 32