python merge split files dailybitalks.com

Handling Large Datasets with Python: Combining and Splitting Files Made Easy

When you work in business intelligence, you inevitably deal with files—lots of them. Sometimes you’ll get a flood of daily CSV exports from your data warehouse. Other times, you’ll inherit a single monster file so large that Excel waves the white flag before it even opens.

The good news? Python makes combining and splitting files much less painful. You don’t need to be a full-blown data engineer to pull this off—just a few lines of code and a little patience.

Let’s walk through two common scenarios:


1. Combining Multiple Files into One

Imagine you receive a folder full of monthly sales reports, each in its own CSV file. Analyzing them one at a time is a waste of time—you want everything in one consolidated dataset.

Here’s a simple Python recipe using pandas:

import pandas as pd
import glob

# Point to your folder of CSVs
path = "data/monthly_reports/"
all_files = glob.glob(path + "*.csv")

# Read and combine them
df_list = [pd.read_csv(file) for file in all_files]
combined_df = pd.concat(df_list, ignore_index=True)

# Save to a single file
combined_df.to_csv("all_reports_combined.csv", index=False)

A few things to note:

  • glob helps you grab every CSV in the folder.
  • pd.concat stacks them together into one tidy DataFrame.
  • Adding ignore_index=True resets the row numbers, so you don’t carry over conflicting indexes from the original files.

Pro tip: if the files all have a date embedded in the filename (like sales_2025_08.csv), you can parse that out and add it as a column before concatenating. That way, your combined dataset includes the time context right away.


2. Splitting a Large File into Smaller Chunks

On the flip side, sometimes the problem is one giant file—maybe an export from your ERP system or a data dump from marketing. Opening it directly in Excel? Forget about it. Even in Python, trying to load 10 million rows in one go can be rough on memory.

Here’s how to split a file into smaller, more manageable pieces:

import pandas as pd

# Load the file in chunks
chunksize = 100000  # adjust to taste
chunk_list = []

for i, chunk in enumerate(pd.read_csv("bigfile.csv", chunksize=chunksize)):
    out_file = f"bigfile_part_{i+1}.csv"
    chunk.to_csv(out_file, index=False)
    print(f"Saved {out_file}")

What’s happening here:

  • chunksize tells pandas to read the file in smaller bites (100,000 rows in this case).
  • Each chunk gets written out as a new file.
  • The enumerate makes sure each file has a unique name like bigfile_part_1.csv, bigfile_part_2.csv, and so on.

This way, instead of choking on one massive file, you get several smaller ones you can open, share, or analyze separately.


Why This Matters for BI Folks

In BI, speed matters—not just in query performance but in how quickly you can get to the analysis stage. Spending hours trying to open unwieldy files or manually merging dozens of exports is energy wasted.

Python gives you a lightweight toolkit to tame the chaos:

  • Consolidation: Roll up daily or monthly files into one dataset you can plug into Power BI or Tableau.
  • Accessibility: Break down huge files into chunks your tools (and laptop) can actually handle.
  • Reusability: Once you write these scripts, they can be reused and tweaked for future projects.

Wrapping Up

You don’t need to become a hardcore developer to make Python part of your BI workflow. Think of it as a power tool in your analyst toolkit—there when you need to wrangle messy files before diving into dashboards and reports.

Next time you’re staring down a folder full of CSVs or a file too big to open, remember: a few lines of Python can save the day.


Discover more from Daily BI Talks

Subscribe to get the latest posts sent to your email.