Combining Series
Series can be joined end to end with concat, or filled in from each other with combine_first.
In this page:
Syntax
combined = pd.concat([s1, s2])
side_by_side = pd.concat([s1, s2], axis=1)
Combining Series
pd.concat([s1, s2]) stacks Series into a longer one, and axis=1 places them side by side as a DataFrame. combine_first fills the gaps in one Series using another. ignore_index=True renumbers the result.
Note:
concat with axis=1 is the quick way to build a DataFrame from several Series.
Example: Combining Series
import pandas as pd
import numpy as np
a = pd.Series([1, 2], index=["x", "y"], name="a")
b = pd.Series([3, 4], index=["y", "z"], name="b")
print(pd.concat([a, b]))
print(pd.concat([a, b], axis=1))
print(pd.Series([1, np.nan, 3]).combine_first(pd.Series([9, 9, 9])))
# Output:
# x 1
# y 2
# y 3
# z 4
# dtype: int64
# a b
# x 1.0 NaN
# y 2.0 3.0
# z NaN 4.0
# 0 1.0
# 1 9.0
# 2 3.0
# dtype: float64
Related Topics
Common Mistakes
- Duplicate index labels after concat
- Forgetting axis=1 for side-by-side
- Expecting concat to modify inputs
Chapter Summary
- concat stacks Series
- axis=1 makes columns
- combine_first fills gaps
- ignore_index renumbers
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: