简体   繁体   中英

string split on multiple delimeters

I have a dataset in which a column info consists of strings like this:

data['info'][0] = 'Banshidhar Roadlines - Ahmedabad Address Opp. Mahadev Avenue, Nr. Sardar Patel Ring Road, Ahmedabad Email : WebSite : City : Ahmedabad, Ahmedabad Pin Code : 382415 State : Gujarat, India Contact No. : 079- - 29292559, 65447413, GST No. : Company Registration No. : About Company Contact Person : Santram B. Yadav Mobile No. : 9376102361, 9377177197, Contact Person : Mobile No. : , , Contact Person : Mobile No. : , , Truck Available : Services : Daily Service :-Gujarat, Maharashtra, Karnataka, Rajasthan, U.P., Bihar & All Over India '

I want to split the string such that I can get data like Email, Website, City, Pin code, etc., each in its own column. I tried using re.split but there are multiple values of Contact Person in the string.

How do I differentiate between them?

So what I did is this:

new = data["info"].str.split("Email :|Address |WebSite :|City :|Pin Code :|State :|Contact No. :|GST No. :|Company Registration No. :|Contact Person :|Mobile No. :|Truck Available :|Services :|Service :", expand=True)

and then assign elements of new to data

data["Address"] = new[1]
data["Email"] = new[2]
data["Website"] = new[3]
data["City"] = new[4]
data["Pin Code"] = new[5]

Like this. and for multiple contact person I did this

data["Contact person 1"] = new[10]
data["p1_contact_no"] = new[11]
data["Contact person 2"] = new[12]
data["p2_contact_no"] = new[13]
data["Contact person 3"] = new[14]
data["p3_contact_no"] = new[15]

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