How can I remove all non-numeric characters from all the values in a particular column in pandas dataframe?

后端 未结 5 1859
清酒与你
清酒与你 2020-11-30 05:43

I have a dataframe which looks like this:

     A       B           C
1   red78   square    big235
2   green   circle    small123
3   blue45  triangle  big657         


        
5条回答
  •  北海茫月
    2020-11-30 06:23

    You can also do this via a lambda function with str.isdigit:

    import pandas as pd
    
    df = pd.DataFrame({'Name': ['John5', 'Tom 8', 'Ron 722']})
    
    df['Name'] = df['Name'].map(lambda x: ''.join([i for i in x if i.isdigit()]))
    
    #   Name
    # 0    5
    # 1    8
    # 2  722
    

提交回复
热议问题