Python操作Excel把数据分给sheet
作者:夏日的向日葵 时间:2023-08-07 17:03:53
需求:根据country列的不同值,将内容分到不同sheet
方法一:
读取原Excel,根据country列将不同的内容放到不同的sheet,并根据国家名称命名,将结果放到新的输出文件中。
#!/usr/bin/env python3
#读取Excel文件
import pandas as pd
input_file = "F://python入门//数据2//appname_test.xlsx"
output_file = "F://python入门//数据2//output.xlsx"
data_frame = pd.read_excel(input_file,sheet_name='sum1',index_col = None)
data_frame_country = data_frame['country']
category_countory = set(data_frame_country)
writer = pd.ExcelWriter(output_file)
for country in list(category_countory):
df = data_frame[data_frame['country'] == country]
df.to_excel(writer, sheet_name= country ,index=False)
writer.save()
结果,生成了output.xlsx,将appname_test.xlsx中的汇总数据根据不同国家分到了不同sheet:
方法二:
读取原Excel,根据country列将不同的内容放到不同的CSV文件,并根据国家名称命名。
#!/usr/bin/env python3
#读取Excel文件
import pandas as pd
input_file = "F://python入门//数据2//appname_test.xlsx"
data_frame = pd.read_excel(input_file,sheet_name='sum1',index_col = None)
data_frame_country = data_frame['country']
category_countory = set(data_frame_country)
for country in list(category_countory):
df = data_frame[data_frame['country'] == country]
df.to_csv("F:/python入门/数据2/table_{}.csv".format(country), encoding="gbk", index=False)
结果生成四个csv文件:
以table_繁体中文为例:
来源:https://www.cnblogs.com/xiao02fang/p/12912685.html
标签:Python,Excel,sheet
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 线程池模块之多线程操作代码
2022-06-14 06:01:47
joomla组件开发入门教程
2024-05-05 09:18:46
Python多线程和队列操作实例
2023-02-06 07:23:54
go常用指令之go mod详解
2024-04-23 09:49:09
![](https://img.aspxhome.com/file/2023/4/134314_0s.png)
Mysql 乘法除法精度不一致问题(除法后四位小数)
2024-01-13 09:44:16
python调用腾讯云实名认证接口辨别身份证真假
2023-07-26 08:20:18
![](https://img.aspxhome.com/file/2023/3/120343_0s.png)
git丢弃本地修改的所有文件(新增、删除、修改)
2023-05-24 17:12:59
Python打印“菱形”星号代码方法
2023-02-05 17:34:42
![](https://img.aspxhome.com/file/2023/5/73005_0s.png)
python中readline判断文件读取结束的方法
2022-12-14 06:22:51
Python run()函数和start()函数的比较和差别介绍
2022-07-04 18:38:47
python多线程扫描端口示例
2022-06-28 23:18:58
Python教程之类型转换详解
2021-03-23 02:48:17
python机器学习实现决策树
2021-04-21 07:44:34
![](https://img.aspxhome.com/file/2023/1/100911_0s.jpg)
asp 过滤非法字符函数
2011-04-07 10:39:00
python实现邮件循环自动发件功能
2021-01-20 02:39:24
![](https://img.aspxhome.com/file/2023/5/104835_0s.png)
PHP的mysqli_stmt_init()函数讲解
2023-06-08 05:04:51
![](https://img.aspxhome.com/file/2023/4/55454_0s.png)
详解微信图片防盗链“此图片来自微信公众平台 未经允许不得引用”的解决方案
2024-04-29 13:25:38
![](https://img.aspxhome.com/file/2023/5/132665_0s.png)
python实现五子棋算法
2022-12-22 18:53:15
![](https://img.aspxhome.com/file/2023/4/133444_0s.gif)
SQL Server 压缩日志与减少SQL Server 文件大小的方法
2024-01-22 04:26:45
Python中ttkbootstrap的介绍与基本使用
2023-08-23 15:57:01
![](https://img.aspxhome.com/file/2023/9/91999_0s.gif)