用Python对Excel数据进行分列处理
2021/10/3 20:11:17
本文主要是介绍用Python对Excel数据进行分列处理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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 |
出处:blog.csdn.net/maymay_/article/details/105361091
这篇关于用Python对Excel数据进行分列处理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python