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.
import pandas as pd
df1 = pd.DataFrame({"a": [1, 2]})
df2 = pd.DataFrame({"a": [3, 4]})
print(pd.concat([df1, df2], ignore_index=True))2Practical Example
Here is a real-world application of pd.concat() showing how it is used in production Pandas code.
import pandas as pd
df1 = pd.DataFrame({"a": [1, 2]})
df2 = pd.DataFrame({"b": [3, 4]})
print(pd.concat([df1, df2], axis=1))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.
import pandas as pd
df1 = pd.DataFrame({"a": [1, 2]})
df2 = pd.DataFrame({"a": [3, 4]})
print(pd.concat([df1, df2], ignore_index=True))