Home > Back-end >  Iterate through rows of pandas df and drop rows which violate rules
Iterate through rows of pandas df and drop rows which violate rules

Time:09-29

I have a df, and I just want to iterate through the rows and if the same value is in the row more than once, drop the row from the df.

Ex: Row1 = 1, 2, 3, 4
    Row2 = 1, 2, 3, 1
    Row3 = 2, 3, 5, 7

For my case, Row2 would be dropped because 1 shows up twice.

I am familiar with iterrows, but I am struggling to form the actual logic needed to accomplish my task.

CodePudding user response:

Try with nunqiue

out = df[df.nunique(1)==df.shape[1]]
Out[97]: 
   1  2  3  4
0  1  2  3  4
2  2  3  5  7
  • Related