0

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+"]
ar_mm18
  • 415
  • 2
  • 8

1 Answers1

3

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+
Naveed
  • 11,495
  • 2
  • 14
  • 21
  • 4
    You could add an anchor to the end of string (`$`) for safety in case there are other parentheses in the string. – mozway Sep 23 '22 at 16:10
  • @ar_mm18, check this out for the regular expression https://stackoverflow.com/questions/4736/learning-regular-expressions – Naveed Sep 23 '22 at 16:25