Fundamentals 4 min read

How to Split Excel Files by Month Using Python Pandas

This article explains how to use Python and pandas to read an Excel workbook, extract the month from a date column, and automatically generate separate Excel files for each month, complete with sample code and result screenshots.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Split Excel Files by Month Using Python Pandas

Introduction

In a Python community a user asked how to split an Excel file into separate files by month based on a date column. The data contains dates from January to August and performance scores.

Implementation

The provided solution uses pandas to read the Excel file, extract the month from the date column, iterate over unique months, and write each subset to a new Excel file.

import pandas as pd

df = pd.read_excel("C:/Users/pdcfi/Desktop/合并表格.xlsx")

df["月份"] = df["日期"].apply(lambda x: x.month)
for month in df["月份"].unique():
    df1 = df[df["月份"] == month]
    df1.to_excel(fr"C:/Users/pdcfighting/Desktop/绩效/" + str(month) + "月份的业绩.xlsx")
    print(f"{month}月份已经拆分完成!")

The script produces separate Excel files for each month, as shown in the result image.

Conclusion

This article demonstrates a practical Python automation technique for splitting Excel data by month, providing clear code and output to help users solve similar office automation tasks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

pandasscript
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.