Python replace non digit character in a dataframe [duplicate]

2024/11/17 15:31:23

I have the following dataframe column

>>> df2['Age]1    25
2    35
3    48 y
4    34 yea
5    29
...

I just want to keep the number en replace the value in df2['Age] like that

1    25
2    35
3    48
4    34
5    29
...

My code doesn't work :

df2.Age.replace('^.*','^[0-9]*[0-9]',regex=True,inplace=True)

here's the result

 1    ^[0-9]*[0-9]2    ^[0-9]*[0-9]3    ^[0-9]*[0-9]4    ^[0-9]*[0-9]5    ^[0-9]*[0-9]...

Thanks for any help in advance

Answer

Use \D+ for replace non numeric to empty string:

df2.Age.replace('\D+','',regex=True,inplace=True)
print (df2)Age
1  25
2  35
3  48
4  34
5  29
https://en.xdnf.cn/q/118793.html

Related Q&A

PyGame.error in ubuntu

I have problem with pygame and python 3 in ubuntu 12.10. When i tried load an image i got this error: pygame.error: File is not a Windows BMP fileBut in python 2.7 all works fine. I use code from http:…

Firebase Admin SDK with Flask throws error No module named firebase_admin

--- Question closedIt was my mistake, my uWSGI startup script switches to a different virtualenv.--- Original questionIm trying to publish push notifications from my Flask app server to Android APP. Se…

Recursive generator for change money - python

First, thanks for any help. I have this recursive code for counting ways of change from a list of coins and a given amount. I need to write a recursive generator code that presents the ways in every it…

Like button not recording the data [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Python, Pygame, Image manipulation: Restretch a loaded png, to be the texture for an isometric Tile

Im a 17 year old programmer, trying to program an isometric game in python, with pygame. After finishing a tile engine, working with not good looking, gimp-drawn PNGs, I wondered, if it would be possib…

TypeError: list object is not callable [duplicate]

This question already has answers here:TypeError: list object is not callable while trying to access a list(9 answers)Closed 10 years ago.I cant find the problem im facing... this is exactly what the …

Tokenise text and create more rows for each row in dataframe

I want to do this with python and pandas.Lets suppose that I have the following:file_id text 1 I am the first document. I am a nice document. 2 I am the second document. I am an even …

Is the example of the descriptor protocol in the Python 3.6 documentation incorrect?

I am new to Python and looking through its documentation I encountered the following example of the descriptor protocol that in my opinion is incorrect. .It looks like class IntField:def __get__(self, …

How to clean a string to get value_counts for words of interest by date?

I have the following data generated from a groupby(Datetime) and value_counts()Datetime 0 01/01/2020 Paul 803 2 01/02/2020 Paul 210982360967 1 …

Folium - Map doesnt appear

I try to get map through Folium but only thing I can see is marker on blank page. Id like to know where is problem lies, in explorer or coding. map.py import foliummap = folium.Map(location = [46.20, 6…