-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

noliverte
  • 191
  • 2
  • 16

2 Answers2

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

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
Rakesh
  • 81,458
  • 17
  • 76
  • 113