Reputation: 33
I have a dataframe of a limited number of rows ~ 500 Max.
I would like to iterate over each row and compare a column's value to the next row.
something like:
for each row in df1
if df1['col1'] of current row == df1['col1'] of current row+1 then drop row
I appreciate your help. Thanks,
Upvotes: 2
Views: 2053
Reputation: 433
I suggest the following :
index_to_drop=[]
for i in range(data.shape[0]-1):
if df.iloc[i]['col1'] == df.iloc[i+1]['col1']:
index_to_drop.append(i)
df.drop(index_to_drop, inplace=True)
Warning : I assume that you have an ordinal encoding of your index (0,1,2,3,4...,n). If it is nit the case, you can do the following beforehand :
df.reset_index(inplace=True)
Upvotes: 1