当前位置: 首页 > news >正文

【Python】多年数据分成不同sheet

需求:
excel文件中包含多年数据,其中一列列名为“年”,要保存一个新excel,将年数值不同的行保存在不同的sheet文件中,每个sheet文件第一行仍为原数据第一行,并且每个sheet名为对应的年的值。

拆分年份数据.py

import pandas as pd# Replace 'input_file.xlsx' and 'output_file.xlsx' with your actual file paths
input_file = 'input_file.xlsx'
output_file = 'output_file.xlsx'# Read the Excel file
df = pd.read_excel(input_file)# Print the column names
print("Column names:", df.columns)# Update the column name based on the actual column name in your Excel file
# For example, if the column name is 'Year', replace '年' with 'Year'
column_name = '年'  # Update this to the actual column name
grouped = df.groupby(column_name)# Create a new Excel writer
with pd.ExcelWriter(output_file, engine='xlsxwriter') as writer:# Iterate over groups and save each group to a separate sheetfor group_name, group_df in grouped:# Create a new sheet with the group namegroup_df.to_excel(writer, sheet_name=str(group_name), index=False, header=True)# Get the worksheet and add a header rowworksheet = writer.sheets[str(group_name)]worksheet.write_string(0, 0, df.columns[0])  # assuming the first column is the header# Print a message
print(f"Data has been saved to {output_file}")

相关文章:

  • Leetcode—2336.无限集中的最小数字【中等】
  • 蓝桥杯day02——Fizz Buzz
  • 超声波清洗机器哪个品牌好用?这四款都夸的超声波清洗机
  • Hive数据库与表操作
  • Git设置多个仓库同时推送
  • Encoder、Decoder和Encoder-Decoder
  • leetcode每日一题35
  • 【matlab程序】图像最大化填充画布
  • PyTorch入门教学——加载数据(Dataset)
  • 智能学习台灯_AI摄像头学习机基于MTk8175方案
  • mysql处理40w数据脚本执行慢问题
  • Clickhouse使用总结
  • Spring Beans;Spring Bean的生命周期;spring Bean的作用域,spring处理线程并发问题
  • 【vue脚手架配置代理+github用户搜索案例+vue项目中常用的发送Ajax请求的库+slot插槽】
  • 【Linux下基本指令——(1)】
  • 收藏网友的 源程序下载网
  • 【划重点】MySQL技术内幕:InnoDB存储引擎
  • Apache的基本使用
  • C++类的相互关联
  • es的写入过程
  • Golang-长连接-状态推送
  • Idea+maven+scala构建包并在spark on yarn 运行
  • JAVA 学习IO流
  • PermissionScope Swift4 兼容问题
  • Python_OOP
  • Quartz初级教程
  • rc-form之最单纯情况
  • Service Worker
  • underscore源码剖析之整体架构
  • V4L2视频输入框架概述
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • Web标准制定过程
  • Web设计流程优化:网页效果图设计新思路
  • 初识 beanstalkd
  • 初识 webpack
  • 分享几个不错的工具
  • 欢迎参加第二届中国游戏开发者大会
  • 基于Mobx的多页面小程序的全局共享状态管理实践
  • 什么是Javascript函数节流?
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • 国内开源镜像站点
  • 好程序员web前端教程分享CSS不同元素margin的计算 ...
  • 树莓派用上kodexplorer也能玩成私有网盘
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • (2)nginx 安装、启停
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (编译到47%失败)to be deleted
  • (附源码)基于SpringBoot和Vue的厨到家服务平台的设计与实现 毕业设计 063133
  • (附源码)计算机毕业设计高校学生选课系统
  • (免费分享)基于springboot,vue疗养中心管理系统
  • (学习日记)2024.01.09
  • (转)Scala的“=”符号简介
  • (转)如何上传第三方jar包至Maven私服让maven项目可以使用第三方jar包