Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

python - How to split based on multiple delimiter pandas

I have a df column which contains

Phone number
12399422/930201021
5451354;546325642
789888744,656313214
123456654

I would like to separate it into two columns

Phone number1      Phone number2
12399422           930201021
5451354            546325642
789888744          656313214
123456654

I have tried to use apply,

    df['TELEPHONE1'] = df['TELEPHONE'].str.split(',').str.get(0)
    df['TELEPHONE2'] = df['TELEPHONE'].str.split(',').str.get(1)
    df['TELEPHONE1'] = df['TELEPHONE'].str.split(';').str.get(0)
    df['TELEPHONE2'] = df['TELEPHONE'].str.split(';').str.get(1)
    df['TELEPHONE1'] = df['TELEPHONE'].str.split('/').str.get(0)
    df['TELEPHONE2'] = df['TELEPHONE'].str.split('/').str.get(1)

but it can only split '/' Thank you for your help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using str.split

df.Phonenumber.str.split('/|;|,',expand=True).add_prefix('Number_')
    Number_0   Number_1
0   12399422  930201021
1    5451354  546325642
2  789888744  656313214
3  123456654       None

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...