简体   繁体   中英

how to split column using python

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM