可以使用iterrows()函数遍历pandas dataframe,并使用shift()函数来比较当前行和前一行的值。如果当前行的值小于前一行的值,则可以使用drop()函数删除该行。
例如,考虑以下示例DataFrame:
import pandas as pd
data = {'A':[10, 5, 8, 6, 12], 'B':[20, 15, 18, 14, 22]}
df = pd.DataFrame(data)
print(df)
输出:
A B
0 10 20
1 5 15
2 8 18
3 6 14
4 12 22
现在,我们将使用iterrows()函数和shift()函数遍历DataFrame,并使用drop()函数删除行:
for index, row in df.iterrows():
if index > 0:
if (row['A'] < df.loc[index-1, 'A']) or (row['B'] < df.loc[index-1, 'B']):
df = df.drop(index)
print(df)
输出:
A B
0 10 20
2 8 18
4 12 22
在此示例中,我们遍历DataFrame并比较当前行的值与前一行的值。如果当前行的任一值都小于前一行的值,则使用drop()函数删除该行。最后,我们打印输出已更新的DataFrame。
请注意,此解决方案仅删除相邻行之间的不合格行。如果要删除连续的不合格行,则需要进行额外的编码。