Extract last part of the string pandas

Viewed 32

I have a column with values:

Dummy Data:

df["temp_design"] = ['Are Premium Design tree (LKL#)',
       'THE Premium Design tree (TKL+)',
       'THZPremium Design tree (TKL+)',
       'THG THEM Entry tree temporary align (MKP#)', nan,
       'THZPremium Design tree (CHU#)',
       'THZPremium Design tree (ZHU2+)',
       'TRUE PREMIUM TEMPORARY DESIGN (ZHU+)',
       'BASIC TEMPORARY DESIGN (ZHU+)']

I want to create a new column that has values present inside the last bracket.

Can anyone help me strip this string?

df["output_col"] =["LKL#","TKL+","TKL+","MKP#","CHU#","ZHU2+","ZHU+","ZHU+"]
1 Answers

use extract with the regex that matches open and close parenthesis and capture the content withinit

df['temp_design'].str.extract(r'\((.*?)\)$') 
    0
0   LKL#
1   TKL+
2   TKL+
3   MKP#
4   NaN
5   CHU#
6   ZHU2+
7   ZHU+
8   ZHU+
Related