I have a dataframe like the following:
df = pd.DataFrame(
data=
[['22 take away', 'something'],
['takeaway 56', 'I see'],
['45 takeaway street', ' This is blue'],
['right street', ' This is white']],
columns=['V1', 'V2']
)
I want to extract the numbers from V1 to a separate variable in that dataframe using regex pattern. I have the following till now:
pattern = r'\d{1,2}'
for i in df.V1:
num = re.search(pattern, i)
if num:
print(num.group(0))
This prints out the numbers but my tries to separate these in a variable are all wrong till now. My goal is to have the following dataframe:
dfgoal = pd.DataFrame(
data=
[['22 take away', 'something', '22'],
['takeaway 56', 'I see', '56'],
['45 takeaway street', ' This is blue', '45'],
['right street', ' This is white', ' ']],
columns=['V1', 'V2', 'V3']
)
Thanks very much!