Home > front end >  Remove rows that contain more than one occurrence of a character (in any column)
Remove rows that contain more than one occurrence of a character (in any column)

Time:05-04

I'm trying to remove rows that contain more than one occurrence of a determined character in any column, but I'm not finding a way of doing that using pandas dataframe. For example:

enter image description here

Would become:

enter image description here

Does anyone know a way to do this? Thank you.

CodePudding user response:

You can use:

>>> df[~df.eq('?').sum(axis=1).gt(1)]
   x  y  z
0  1  2  ?
1  3  2  1
3  2  3  1

CodePudding user response:

If you're looking for any duplicates including the numbers, you can try:

>>> df.loc[~df.T.apply(pd.Series.duplicated, keep=False).T.any(axis=1)]
   x  y  z
0  1  2  ?
1  3  2  1
3  2  3  1
  • Related