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
167 views
in Technique[技术] by (71.8m points)

python - Identify integers in column and replace with string

I'm new to python and pandas. I have a dataframe that looks as follows:

             ID                    NAME
0      0000001                    Apple
1      0000002                       35
2      0000003                    Grape
3      0000004                       22
4      0000005                   Banana
5      0000006                       12

My goal is to replace the fields with numbers in the NAME column with 'Unknown'

So far, I have tried the following:

out['NAME'] = out.apply(lambda x: x['NAME'].replace(x['NAME'], 'Unknown'))

But it wouldn't seem to replace it and gives me a KeyError: ('NAME', 'occurred at index ID')

Ultimately I am expecting an output as follows:

             ID                    NAME
0      0000001                    Apple
1      0000002                   Unkown
2      0000003                    Grape
3      0000004                   Unkown
4      0000005                   Banana
5      0000006                   Unkown
question from:https://stackoverflow.com/questions/65909227/identify-integers-in-column-and-replace-with-string

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

1 Reply

0 votes
by (71.8m points)
out['NAME']=np.where(out.NAME.str.contains('d'),'unknown',out.NAME)

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

...