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