Question: I have a data -

```
data = [['0092', 'ab cd [1]'], ['887', 'ab ef [2]'], ['776', 'ab gh ab [3]'], 
        ['33101', 'ab ij [4]'], ['0922', 'ab kl [5]']]
df = pd.DataFrame(data, columns = ('col_1', 'col_2'))
df

   col_1    col_2
    0092    ab cd [1]
    887     ab ef [2]
    776     ab gh ab [3]
    33101   ab ij [4]
    0922    ab kl [5]

```

I want to split col\_2 and create a new column where "ab cd" is a name and 1 is its code.

```
       col_1     col_2    col_3
        0092    ab cd       1
        887     ab ef       2
        776     ab gh ab    3
        33101   ab ij       4
        0922    ab kl       5

```

I have tried this - `df['col_2'].str.split(" ", expand = True)` but it doesn't serve my purpose as it doesn't split the names accurately in terms of space occurrence. So what i was thinking to split names from starting alphabet till " [" and then create a new column based on the split. If you have any better suggestions(fewer lines of code) then please help me to code it

Answer: 
We can use `str.extract` here:

```py
df["col_3"] = df["col_2"].str.extract(r'^.*\[(\d+)\]')
df["col_2"] = df["col_2"].str.extract(r'^(.*?)(?=\s*(?:\[\d+\]|$))')

```
You can use:

```
df['col_2'] = df['col_2'].str.replace(r"[[0-9]+]$","")
df['col_3'] = df['col_2'].str.extract('(\d+)')

```