Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 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 Reply

0 votes
by (71.8m points)

Try using .map() for your task:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...