-1

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

0

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

2

Using str.extract

Ex:

import pandas as pd

df = pd.DataFrame({"Age": ['25', '35', '48 y', '34 yea', '29']})
df["Age"] = df["Age"].str.extract(r"(\d+)", expand=False)
print(df)

Output:

  Age
0  25
1  35
2  48
3  34
4  29

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.