Monthly exports often arrive as separate tables even though every file describes the same kind of record. A single combined pandas DataFrame gives later filters, summaries, and exports one continuous set of rows.
The pandas.concat() function accepts a sequence of DataFrame objects and stacks rows along axis=0 by default. Collecting the frames before one call avoids copying an expanding result on every iteration.
This row-stacking workflow assumes that the monthly tables represent the same columns. ignore_index=True replaces reused source labels with a fresh integer index, while explicit assertions stop the program if the row count, index, or record order differs from the expected result.
Related: How to create a pandas DataFrame
Related: How to merge pandas DataFrames
Related: How to read CSV files with pandas
Steps to concatenate pandas DataFrames:
- Create concat_dataframes.py with the monthly order tables.
- concat_dataframes.py
import pandas as pd january = pd.DataFrame( { "order_id": ["A100", "A101"], "region": ["EMEA", "APAC"], "total": [125.0, 98.5], } ) february = pd.DataFrame( { "order_id": ["A102", "A103"], "region": ["EMEA", "AMER"], "total": [143.0, 87.0], } )
Loaded extracts need the same column meanings, and their sequence determines the combined row order.
- Add the concatenation section below the February table.
monthly_orders = [january, february] orders = pd.concat(monthly_orders, ignore_index=True)
ignore_index=True discards labels on the row axis and creates a fresh RangeIndex. pandas.merge() matches records by key values instead of stacking rows.
- Append the result assertions after the concat call.
expected_ids = ["A100", "A101", "A102", "A103"] assert orders.shape == (4, 3) assert orders.index.tolist() == [0, 1, 2, 3] assert orders["order_id"].tolist() == expected_ids
- Append the result display after the assertions.
print(orders) print(f"shape={orders.shape}") print(f"index={orders.index.tolist()}") print(f"order_ids={orders['order_id'].tolist()}")
- Run the completed concat program to confirm the combined table.
$ python3 concat_dataframes.py order_id region total 0 A100 EMEA 125.0 1 A101 APAC 98.5 2 A102 EMEA 143.0 3 A103 AMER 87.0 shape=(4, 3) index=[0, 1, 2, 3] order_ids=['A100', 'A101', 'A102', 'A103']
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.