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

后端 未结 5 1915
清酒与你
清酒与你 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:20

    Use str.extract and pass a regex pattern to extract just the numeric parts:

    In[40]:
    dfObject['C'] = dfObject['C'].str.extract('(\d+)', expand=False)
    dfObject
    
    Out[40]: 
            A         B    C
    1   red78    square  235
    2   green    circle  123
    3  blue45  triangle  657
    

    If needed you can cast to int:

    dfObject['C'] = dfObject['C'].astype(int)
    

提交回复
热议问题