🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

pd.concat()

AI & DATA SCIENCE // pd-concat

pd.concat() stacks a sequence of DataFrames or Series together, either along rows (appending one after another) or along columns (placing them side by side).

Syntax

pd.concat(objs, axis=0, ignore_index=False)

Deep Dive Course

concat() with the default axis=0 stacks DataFrames on top of each other, appending rows, and aligns them by matching column names, filling in NaN for any column present in one but not another; axis=1 instead places them side by side as new columns, aligning by matching index labels. Unlike merge(), concat() doesn't try to match rows based on shared key values at all — it's a purely positional/structural combination, not a relational join.

1Understanding pd.concat()

concat() with the default axis=0 stacks DataFrames on top of each other, appending rows, and aligns them by matching column names, filling in NaN for any column present in one but not another; axis=1 instead places them side by side as new columns, aligning by matching index labels. Unlike merge(), concat() doesn't try to match rows based on shared key values at all — it's a purely positional/structural combination, not a relational join.

💡

Pass ignore_index=True to concat() when stacking DataFrames by rows if the original index values aren't meaningful — otherwise the combined result keeps each piece's original index, which commonly produces duplicate index labels across the combined DataFrame.

editor.html
import pandas as pd

df1 = pd.DataFrame({"a": [1, 2]})
df2 = pd.DataFrame({"a": [3, 4]})
print(pd.concat([df1, df2], ignore_index=True))
localhost:3000

2Practical Example

Here is a real-world application of pd.concat() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df1 = pd.DataFrame({"a": [1, 2]})
df2 = pd.DataFrame({"b": [3, 4]})
print(pd.concat([df1, df2], axis=1))
localhost:3000

3Best Practices

Follow these guidelines when working with pd.concat():

1. Pass ignore_index=True when combining DataFrames by row and the original index values don't carry meaning, to avoid ending up with duplicate index labels

2. Use concat() for simply stacking or side-by-side placing DataFrames with no key-based matching needed, reserving merge() for actual relational joins

3. Check for unexpected NaN values after concat() with mismatched columns, which signals the pieces didn't actually share identical column sets

⚠️

Tip: Pass ignore_index=True to concat() when stacking DataFrames by rows if the original index values aren't meaningful — otherwise the combined result keeps each piece's original index, which commonly produces duplicate index labels across the combined DataFrame.

editor.html
import pandas as pd

df1 = pd.DataFrame({"a": [1, 2]})
df2 = pd.DataFrame({"a": [3, 4]})
print(pd.concat([df1, df2], ignore_index=True))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df1 = pd.DataFrame({"a": [1, 2]})
df2 = pd.DataFrame({"a": [3, 4]})
print(pd.concat([df1, df2], ignore_index=True))
Example 02Advanced Example
import pandas as pd

df1 = pd.DataFrame({"a": [1, 2]})
df2 = pd.DataFrame({"b": [3, 4]})
print(pd.concat([df1, df2], axis=1))

Best Practices

  • Pass ignore_index=True when combining DataFrames by row and the original index values don't carry meaning, to avoid ending up with duplicate index labels
  • Use concat() for simply stacking or side-by-side placing DataFrames with no key-based matching needed, reserving merge() for actual relational joins
  • Check for unexpected NaN values after concat() with mismatched columns, which signals the pieces didn't actually share identical column sets

Interview Question

Why does concat() sometimes introduce NaN values when combining DataFrames along rows (axis=0)?

Hint: Think about what happens when the DataFrames being stacked don't have identical columns.

When stacking DataFrames along rows, concat() aligns them by column name, similar to how a Series operation aligns by index label — if one DataFrame has a column that another doesn't, the rows coming from the DataFrame missing that column get NaN filled in for it in the combined result, since there's no actual data to put there. This is concat()'s way of gracefully handling DataFrames with different column sets rather than raising an error, but it means any accidental column-name mismatch, like a typo or inconsistent capitalization between the pieces, silently produces unwanted NaN values instead of a clean stack.

Exercises

MediumPractice using pd.concat() in a real scenario.
View Solution
import pandas as pd

df1 = pd.DataFrame({"a": [1, 2]})
df2 = pd.DataFrame({"a": [3, 4]})
print(pd.concat([df1, df2], ignore_index=True))

Frequently Asked Questions

Why does concat() sometimes introduce NaN values when combining DataFrames along rows (axis=0)?

When stacking DataFrames along rows, concat() aligns them by column name, similar to how a Series operation aligns by index label — if one DataFrame has a column that another doesn't, the rows coming from the DataFrame missing that column get NaN filled in for it in the combined result, since there's no actual data to put there. This is concat()'s way of gracefully handling DataFrames with different column sets rather than raising an error, but it means any accidental column-name mismatch, like a typo or inconsistent capitalization between the pieces, silently produces unwanted NaN values instead of a clean stack.

Related Functions

pd-mergedf-joindf-fillna