🎓 A1Learner
🔍
0 of 50 lessons completed

Stacking Tables

concat stacks tables on top of each other. The result holds the rows of both tables.

Stack two frames

concat stacks the February rows under the January rows. The old labels repeat, 0 and 1 twice.

import pandas as pd

jan = pd.DataFrame({
    "product": ["Tea", "Coffee"],
    "units": [2, 1],
})
feb = pd.DataFrame({
    "product": ["Mug", "Pen"],
    "units": [3, 5],
})
print(pd.concat([jan, feb]))

Fresh index numbers

ignore_index=True numbers the rows fresh from 0. The labels now read 0, 1, 2, 3.

import pandas as pd

jan = pd.DataFrame({
    "product": ["Tea", "Coffee"],
    "units": [2, 1],
})
feb = pd.DataFrame({
    "product": ["Mug", "Pen"],
    "units": [3, 5],
})
print(pd.concat([jan, feb], ignore_index=True))

Example

import io
import pandas as pd

SALES_CSV = """order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""

SALES_FEB_CSV = """order_id,date,product,category,units,price,region
201,2026-02-03,Tea,Drink,3,4.50,North
202,2026-02-04,Mug,Home,2,12.00,South
203,2026-02-05,Pen,Office,4,2.00,East
204,2026-02-06,Coffee,Drink,1,6.00,West"""

jan = pd.read_csv(io.StringIO(SALES_CSV))
feb = pd.read_csv(io.StringIO(SALES_FEB_CSV))
both = pd.concat([jan, feb], ignore_index=True)
print(both.shape)
print(both.tail(4))

Key points

  • Use pd.concat to stack two DataFrames into one.
  • Pass a list of frames to concat, in the order they should stack.
  • The columns must match for the rows to line up.
  • Pass ignore_index=True to number the rows fresh from 0.

In this example, January sales load from one CSV text and February sales from another. concat stacks the February rows under the January rows. The result holds 16 rows, and the tail shows the four February orders.

Note

concat keeps the old index labels unless ignore_index=True is passed.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

Which call stacks frame feb under frame jan with fresh index numbers?