jcoke
jcoke

Reputation: 1891

Loop over a column and remove characters - Python Pandas

This may be really trivial but I'm struggling to get anywhere with it.

So basically I have a column of ID's where some values are alphanumeric and some contain text prefixed before the alphanumeric value. How do I loop through that specific column and check whether the alphanumeric value has been prefixed with a string and then remove that string accordingly.

This is the way I was doing it but it's not working, nor is it the smart way to do it:

document[ID] = document[ID].replace("ReferenceNode, Objectid:", '')

Example:

ID
5d61527f0928c99f3cf10829
5d61527f0928c99f3cf10829
ReferenceNode, ObjectID: 5d61527f0928c99f3cf10829

Expected Output:

ID
5d61527f0928c99f3cf10829
5d61527f0928c99f3cf10829
5d61527f0928c99f3cf10829

Upvotes: 4

Views: 785

Answers (1)

Punker
Punker

Reputation: 1878

Try using .map() for your task:

document['ID'] = document['ID'].map(lambda x: x.replace("ReferenceNode, Objectid:", ''))

Upvotes: 4

Related Questions