How to improve regex while fetching record dynamically
The data is as follows:
COL1 COL2
12 :402:agsh,hhjd,:45:hghgh,gruru,:12:fgh,ghgh,:22:hhhh
57 :42:agshhhjd,:57:hghgh,gruru,:120:fghghgh,:12:hhhhhh
I am creating a third column field_info
like:
COL1 COL2 field_info
12 :402:agsh,hhjd,:45:hghghgruru,:12:fgh,ghgh,:22:hhhh fgh,ghg
57 :42:agshhhjd :57:hghgh,gruru:120:fghghgh :12:hhhhhh hghgh,g
I am using a regex function as follows:
df.loc[:,'field_info']=df.col2.replace(regex=r'.*'+ df.col1.astype('str') +':(.{15}).*',value="\\1")
I have 2 columns col1 col2. col1 has some value which I am searching in col2 dynamically and extracting the next 15 characters from that. However, it's taking a lot of time. Can anyone suggest a faster way of doing this?
Category Data Science