Extract a certain part of a string after a key phrase using pandas? Extract a certain part of a string after a key phrase using pandas? pandas pandas

Extract a certain part of a string after a key phrase using pandas?


You can use the Series str.extract string method:

In [11]: df = pd.DataFrame([["(12:25) (No Huddle Shotgun) P.Manning pass short left to W.Welker pushed ob at DEN 34 for 10 yards (C.Graham)."]])In [12]: dfOut[12]:                                                   00  (12:25) (No Huddle Shotgun) P.Manning pass sho...

This will "extract" what's it the group (inside the parenthesis):

In [13]: df[0].str.extract("for (\d+)")Out[13]:0    10Name: 0, dtype: objectIn [14]: df[0].str.extract("for (\d+) yards")Out[14]:0    10Name: 0, dtype: object

You'll need to convert to int, e.g. using astype(int).


This will grab the number 10 and put it in a variable called yards.

x = "(12:25) (No Huddle Shotgun) P.Manning pass short left to W.Welker pushed ob at DEN 34 for 10 yards (C.Graham)."

yards = (x.split("for ")[-1]).split(" yards")[0]