I am trying to split a column based on type. I want to show numbers separate from text.
I have tried to add it without a loop, but the shape is different. I therefore resorted to loop it through. It is however only giving me the last number in all fields
Python input:
newdf = pd.DataFrame()
newdf['name'] = ('leon','eurika','monica','wian')
newdf['surname'] = ('swart38','39swart','11swart','swart10')
a = newdf.shape[0]
newdf['age'] = ""
for i in range (0,a):
newdf['age'] = re.sub(r'\D', "",str(newdf.iloc[i,1]))
print (newdf)
I am expecting the age column to show 38,39,11,10
. The answer is however all "10"
being the last field.
Out:
name surname age
0 leon swart38 10
1 eurika swart39 10
2 monica 11swart 10
3 wian swart10 10