我想在满足条件时删除行: 我需要删除特定列中小于零的任何值。
例如,我导入一个csv文件:
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
运行此代码后,我除去了drop1差值为1的大多数行。但是我仍然没有什么负面价值。
我希望代码重复该过程,直到该列只有正值为止
答案 0 :(得分:0)
删除循环语句并将此行放在下面:
df.drop(df[df['Difference1'] < 0].index, inplace=True)