How to Dynamically Remove Dataframe Rows Based On Conditions
Python
In this example we iterate through all rows in a Pandas dataframe and remove any rows where the condition "col_2" is greater than or equal to "col_1" is met.
1| INPUT: 2| 3| col_1 col_2 4| 0 43 120 5| 1 25 3 6| 2 67 19 7| 3 86 34 8| 9| CODE: 10| 11| df.reset_index(inplace=True, drop=True) 12| for i in range(0, len(df) - 1): 13| if df.loc[i, 'col_2'] >= df.loc[i, 'col_1']: 14| df.drop(i, inplace=True) 15| 16| OUTPUT: 17| 18| col_1 col_2 19| 1 25 3 20| 2 67 19 21| 3 86 34
131
118
115
108