1

I have a dataframe in python with an update column containing text. In each row of this column there is a value between parenthesis that I want to grab and place in a new column.

So if it says "Day To Day (Hand) - Bembry did not play" I want to take Hand and add it to a new column named Injury in the same df

See the code below:

display(df.dtypes)

Team      object
Date      object
Update    object
dtype: object

display(df.head())
    Team    Date    Update
0   Atlanta Hawks   Fri, Feb 7, 2020    Day To Day (Hand) - Bembry did not play in Fri...
1   Atlanta Hawks   Sat, Feb 8, 2020    Out (Right Calcaneus) - Capela is out for Sund...
2   Atlanta Hawks   Sat, Feb 8, 2020    Day To Day (Calf) - Fernando is probable for S...
3   Atlanta Hawks   Sat, Feb 8, 2020    Day To Day (Right Knee) - Graham is probable f...
4   Atlanta Hawks   Sat, Feb 8, 2020    Day To Day (Left Ankle) - Hunter is questionab...


Would someone be able to point me in the right direction? Thanks in advance.

2 Answers2

2

Try:

df["Injury"] = df["Update"].apply(lambda x: x[x.find("(") + 1 : x.find(")")])

What .apply does is it takes the "Update" column and runs each value inside through a function, and returns the result. In this case that function finds the text between the parentheses. Then you can just set the new column to that returned value.

Oliver Ni
  • 2,606
  • 7
  • 29
  • 44
0

Try:

df["Injury"]=df["Update"].str.extract(r"[(]([^)]*)[)]")

It will return the content of first encountered square brackets inside of Update column.

Ref: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.extract.html

If you wish to have more than one per line - consider using pd.Series.extractall(...)

Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34