Reputation: 85
import pandas as pd
df = pd.DataFrame({'input':['Age is 55 and 5 years', 'Age is 58','Age is 60.5','Age is 12 and 5 years',
'Age is 30.5 and 2 years of service']})
I need the desired output like below:
output:
=======
55,5
58
60.5
12,5
30.5,2
Tried code:
df = df.assign(A = lambda x: x['input'].str.extract('(\d+)'))
Upvotes: 1
Views: 35
Reputation: 26676
Please use findall
#df=df.assign(A=df.input.str.findall('(\d+)'))#If you dont mind having them in list
df=df.assign(A=df.input.str.findall('(\d+)').str.join(','))#Rid them off the corner brackets
input A
0 Age is 55 and 5 years 55,5
1 Age is 58 58
2 Age is 60.5 60,5
3 Age is 12 and 5 years 12,5
4 Age is 30.5 and 2 years of service 30,5,2
Upvotes: 3