Rob Porter
Rob Porter

Reputation: 81

Apply regular expression to a pandas dataframe column

Im trying to apply some regular expressions that I have coded up and can run against a variable but I would like to apply it on a dataframe column and then pass the results out to a new column

df["Details"] is my dataframe

df["Details"] is my dataframe and it contains some text similar to what I have created below as details

import re
details = '1st: Batman 01:12.98 11.5L'

position = re.search('\w\w\w:\s', details)
distance = re.search('(\s\d\d.[0-9]L)', details)
time = re.search(r'\d{2}:\d{2}.\d{2}',details)

print(position.group(0))
print(distance.group(0))
print(time.group(0))
output is then 
    1st: 
    11.5L
    01:12.98

I would like to then be able to add those values to new columns in the dataframe called position,distance,time respectively matching the output

Upvotes: 2

Views: 5450

Answers (2)

tawab_shakeel
tawab_shakeel

Reputation: 3739

Apply extract in lambda function:

df['position'] = df['Details'].apply(lambda x: str(x).extract(r'(\w\w\w:\s)')))
df['distance'] = df['Details'].apply(lambda x: str(x).extract(r'(\s\d\d.[0-9]L)'))
df['time'] = df['Details'].apply(lambda x: str(x).extract(r'(\d{2}:\d{2}.\d{2})'))

Upvotes: 0

jezrael
jezrael

Reputation: 862441

I believe you need Series.str.extract:

details = '1st: Batman 01:12.98 11.5L'

df = pd.DataFrame({"Details":[details,details,details]})

df['position'] = df['Details'].str.extract(r'(\w\w\w:\s)')
df['distance'] = df['Details'].str.extract(r'(\s\d\d.[0-9]L)')
df['time'] = df['Details'].str.extract(r'(\d{2}:\d{2}.\d{2})')
print(df)

                      Details position distance      time
0  1st: Batman 01:12.98 11.5L    1st:     11.5L  01:12.98
1  1st: Batman 01:12.98 11.5L    1st:     11.5L  01:12.98
2  1st: Batman 01:12.98 11.5L    1st:     11.5L  01:12.98

Upvotes: 8

Related Questions