字符串在多个定界符上分割

时间:2019-03-31 05:30:00

标签: python regex python-3.x string pandas

我有一个数据集,其中 info 列由这样的字符串组成:

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 '

我想分割字符串,这样我就可以在自己的列中获取诸如电子邮件,网站,城市,个人识别码等数据。我尝试使用re.split,但是字符串中有 Contact Person 的多个值。

如何区分它们?

1 个答案:

答案 0 :(得分:1)

所以我是这样做的:

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)

然后将new元素分配给数据

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

赞。而对于多个联系人,我做到了

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]