Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

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
question from:https://stackoverflow.com/questions/65935190/loop-over-a-column-and-remove-characters-python-pandas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try using .map() for your task:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...