当前位置:首页 >> 编程语言 >> 【Python】多年数据分成不同sheet,酷派5890(python将数据分类)

【Python】多年数据分成不同sheet,酷派5890(python将数据分类)

0evadmin 编程语言 1
文件名:【Python】多年数据分成不同sheet,酷派5890 【Python】多年数据分成不同sheet

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

拆分年份数据.py

import pandas as pd# Replace 'input_file.xlsx' and 'output_file.xlsx' with your actual file pathsinput_file = 'input_file.xlsx'output_file = 'output_file.xlsx'# Read the Excel filedf = pd.read_excel(input_file)# Print the column namesprint("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 namegrouped = df.groupby(column_name)# Create a new Excel writerwith 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 messageprint(f"Data has been saved to {output_file}")
协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接