Home > other >  how to split column using python
how to split column using python

Time:10-25

My input is as below

Column A
ABC-5678-1/15/1
ABCD-7589-1/8/2
AB-78698-1/10/1

and required output is (only want to taken value between two /)

Column A
ABC-5678-15
ABCD-7589-8
AB-78698-10

CodePudding user response:

You can use str.replace with a regex, capturing groups, and references:

df['Column A'].str.replace(r'^([^/] -)[^-/] /([^/] )/.*', r'\1\2', regex=True)

Output:

0    ABC-5678-15
1    ABCD-7589-8
2    AB-78698-10

CodePudding user response:

I would use str.replace as follows:

df['Column A'] = df['Column A'].str.replace(r'/\d $', '')

Here is a demo showing that the regex replacement is working.

  • Related