We use concat() to join two or more tables (DataFrames) together. It helps us combine data from different sources into one big table.
0
0
concat() for stacking DataFrames in Pandas
Introduction
You have sales data from January and February in separate tables and want to see all months together.
You collected survey results from different groups and want to combine them into one list.
You want to add new rows of data to an existing table without changing the columns.
You want to stack data vertically or horizontally to compare or analyze together.
Syntax
Pandas
import pandas as pd # Concatenate DataFrames vertically (stack rows) combined_df = pd.concat([df1, df2], axis=0, ignore_index=True) # Concatenate DataFrames horizontally (stack columns) combined_df = pd.concat([df1, df2], axis=1)
axis=0 stacks rows (one below the other).
axis=1 stacks columns (side by side).
Examples
Stack two DataFrames with the same columns one below the other.
ignore_index=True resets the row numbers.Pandas
import pandas as pd df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]}) # Vertical concat (stack rows) result = pd.concat([df1, df2], axis=0, ignore_index=True) print(result)
Stack two DataFrames side by side, adding columns.
Pandas
import pandas as pd df1 = pd.DataFrame({'A': [1, 2]}) df2 = pd.DataFrame({'B': [3, 4]}) # Horizontal concat (stack columns) result = pd.concat([df1, df2], axis=1) print(result)
Concatenating with an empty DataFrame just returns the non-empty one.
Pandas
import pandas as pd # Edge case: empty DataFrame empty_df = pd.DataFrame() df = pd.DataFrame({'A': [1, 2]}) result = pd.concat([empty_df, df], axis=0, ignore_index=True) print(result)
Concatenating DataFrames with only one row each stacks them correctly.
Pandas
import pandas as pd df_single = pd.DataFrame({'A': [10]}) df_another = pd.DataFrame({'A': [20]}) # Concatenate single-row DataFrames result = pd.concat([df_single, df_another], axis=0, ignore_index=True) print(result)
Sample Program
This program creates two small tables for sales in January and February. Then it stacks them vertically to see all sales together.
Pandas
import pandas as pd # Create two DataFrames with sales data sales_jan = pd.DataFrame({ 'Product': ['Apple', 'Banana'], 'Sales': [100, 150] }) sales_feb = pd.DataFrame({ 'Product': ['Apple', 'Banana'], 'Sales': [120, 130] }) print('Sales January:') print(sales_jan) print('\nSales February:') print(sales_feb) # Combine sales data vertically (stack rows) all_sales = pd.concat([sales_jan, sales_feb], axis=0, ignore_index=True) print('\nAll Sales Combined:') print(all_sales)
OutputSuccess
Important Notes
Time complexity: Concatenation takes time proportional to the total size of all DataFrames combined.
Space complexity: Requires memory to hold all combined data.
Common mistake: Forgetting ignore_index=True can keep old row numbers, causing duplicate indices.
Use concat() to stack data vertically or horizontally. For merging based on keys, use merge() instead.
Summary
concat() joins DataFrames by stacking rows or columns.
Use axis=0 to stack rows, axis=1 to stack columns.
Remember to reset index with ignore_index=True when stacking rows.