This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Find consecutive duplicate strings in rows from df

I have a list of annotated protein sequences with their corresponding IDs. I am trying to create a function that detects consecutive duplicate entries in the first column (protein ID) and returns false or true. I tried this:

df = pd.read_csv('taxonomy.tsv', sep='\t', decimal='.')
value = df.iloc[:, 1].diff().lt(0)
print (value)

I obtain the following error:

TypeError: unsupported operand type(s) for -: 'str' and 'str'

Do you know how can I fix it?

Thank you.

python metagenomics pandas

2 answers

l= list(df.iloc[:,1])
r=[False]
for k in range(1,len(l)):
    r.append(l[k]==l[k-1])
print(r)

This should do the trick

Hi! Thanks! I tried your method and obtained the following error: TypeError: '(slice(None, None, None), 1)' is an invalid key

it'll return all duplicate rows back

df[df.duplicated(['protein ID'], keep=False)]['protein ID']

Log in to answer this question.