热线电话:13121318867

登录
2020-06-20 阅读量: 972
python中如何将字符串按指定的分隔符拆分?

 series中的元素均为字符串时,通过str.split可将字符串按指定的分隔符拆分成若干列的形式。

image.png

例如:拆分以逗号为分隔符的字符串

 1 # -*- coding: utf-8 -*-  
 2 # 创建dataframe  
 3 import pandas as pd  
 4 s = pd.DataFrame(['a,b,c','c,d,e'])  
 5 print(s)  
 6 """  
 7        0  
 8 0  a,b,c  
 9 1  c,d,e 
 10 """ 
 11 # 字符串拆分--expend = False 
 12 temp_expend_False = s[0].str.split(',') 
 13 print(temp_expend_False) 
 14 """ 
 15 0    [a, b, c] 
 16 1    [c, d, e] 
 17 """ 
 18  
 19 # 字符串拆分--expend = True 
 20 temp_expend_True = s[0].str.split(',',expand = True) 
 21 print(temp_expend_True) 
 22 """ 
 23    0  1  2 
 24 0  a  b  c 
 25 1  c  d  e 
 26 """


29.1962
2
关注作者
收藏
评论(0)

发表评论

暂无数据