split用法
以下實例展示了 split() 函數的使用方法:
#!/usr/bin/python3
str = "this is string example....wow!!!"
print (str.split( )) # 以空格為分隔符
print (str.split('i',1)) # 以 i 為分隔符
print (str.split('w')) # 以 w 為分隔符
以上實例輸出結果如下:
['this', 'is', 'string', 'example....wow!!!']
['th', 's is string example....wow!!!']
['this is string example....', 'o', '!!!']
pandas 分列
pandas對文本列進行分列,非常簡單:
import pandas as pd
df10 = pd.DataFrame({'姓名':['張三', '李四','王五'] ,
"科目":['語文,100','語文,86','語文,96']})
df10
姓名 | 科目 | |
---|---|---|
0 | 張三 | 語文,100 |
1 | 李四 | 語文,86 |
2 | 王五 | 語文,96 |
res = df10["科目"].str.split(',',expand= True)
res
0 | 1 | |
---|---|---|
0 | 語文 | 100 |
1 | 語文 | 86 |
2 | 語文 | 96 |
df10[["科目",'分數']]=res
df10
姓名 | 科目 | 分數 | |
---|---|---|---|
0 | 張三 | 語文 | 100 |
1 | 李四 | 語文 | 86 |
2 | 王五 | 語文 | 96 |
DataFrame.str.split() :
對文本列分列,第一參數指定分隔符
參數 expand ,表示是否擴展成列,若設置為 True ,則分割后的每個元素都成為單獨一列
出處:blog.csdn.net/maymay_/article/details/105361091