The Pandas concat() function
pd.concat() is a function in the Pandas library used for **concatenating (joining) multiple Series or DataFrames**. It can merge multiple objects into a new one along rows or columns.
It is one of the most common merge operations in data processing, whether appending data vertically or merging fields horizontally,pd.concat() it can handle both.
Word meaning: concat is short for "concatenate", meaning to join multiple things end to end.
Basic syntax and parameters
pd.concat() is a top-level function of the Pandas library used to concatenate multiple Series or DataFrame objects along a specified axis.
Syntax format
pd.concat(objs, axis=0, join='outer', ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=False)
Parameter Description
- Parameters:
objs - Type: Series, DataFrame, or a list of them.
- Description: One or more Series/DataFrame objects to concatenate. Can be a single object, a list, or a dictionary.
- Parameters:
axis - Type: Integer (0 or 1).
- Description: Specifies the axis along which to concatenate.
0means concatenating by rows (vertical append),1means concatenating by columns (horizontal merge). The default is0。
- Parameters:
join - Type: String ('outer' or 'inner').
- Description: How to handle mismatched indexes along the concatenation axis.
'outer'means take the union (keep all indices),'inner'means take the intersection (keep only common indices). Defaults to'outer'。
- Parameters:
ignore_index - Type: boolean.
- Description> If
True, the original indices are not used after concatenation; instead a new integer index is created. Defaults toFalse。
- Parameters:
keys - Type> sequence.
- Description: Used to create a multi-level index after concatenation, identifying the source of each original object. Defaults to
None。
Function description
- Return Value: Returns the concatenated Series or DataFrame object. The type matches the input object.
- effect: Combines multiple objects into one along the specified axis.
Example
Let us thoroughly master pd.concat() through a series of examples from simple to complex.
Example 1: Basic usage - vertically concatenate Series
Example
import
pandas
as
pd
1. Create two Series
s1
=
pd.
Series
(
[
'Alice'
,
'Bob'
,
'Charlie'
]
,
name
=
'name'
)
s2
=
pd.
Series
(
[
'Diana'
,
'Eve'
,
'Frank'
]
,
name
=
'name'
)
(
"=== Original Series ==="
)
(
"s1:"
,
s1.
tolist
(
)
)
(
"s2:"
,
s2.
tolist
(
)
)
2. Use pd.concat() to concatenate vertically (default axis=0)
result
=
pd.
concat
(
[
s1
,
s2
]
)
(
"\n=== pd.concat([s1, s2]) vertical concatenation result ==="
)
(
result
)
3. Ignore the original indices and create a new integer index
result_ignore
=
pd.
concat
(
[
s1
,
s2
]
,
ignore_index
=
True
)
(
"\n=== ignore_index=True result ==="
)
(
result_ignore
)
Expected output:
=== 原始 Series ===
s1: ['Alice', 'Bob', 'Charlie']
s2: ['Diana', 'Eve', 'Frank']
=== pd.concat([s1, s2]) 纵向拼接结果 ===
0 Alice
1 Bob
2 Charlie
0 Diana
1 Eve
2 Frank
dtype: object
=== ignore_index=True 结果 ===
0 Alice
1 Bob
2 Charlie
3 Diana
4 Eve
5 Frank
dtype: object
Code analysis:
pd.concat([s1, s2])Concatenate two Series vertically into a longer Series.- By default, the original indices are kept, so there will be duplicate index values 0, 1, 2.
- Settings
ignore_index=TrueYou can regenerate a consecutive numeric index.
Example 2: Horizontally concatenate DataFrame
Use axis=1 You can concatenate multiple DataFrames by column, which is useful when merging different fields.
Example
import
pandas
as
pd
1. Create two DataFrames
df1
=
pd.
DataFrame
(
{
'name'
:
[
'Alice'
,
'Bob'
,
'Charlie'
]
,
'age'
:
[
25
,
30
,
35
]
}
)
df2
=
pd.
DataFrame
(
{
'city'
:
[
'Beijing'
,
'Shanghai'
,
'Guangzhou'
]
,
'score'
:
[
85
,
90
,
95
]
}
)
(
"=== Original DataFrame ==="
)
(
"df1:"
)
(
df1
)
(
"\ndf2:"
)
(
df2
)
2. Concatenate horizontally (axis=1)
result
=
pd.
concat
(
[
df1
,
df2
]
,
axis
=
1
)
(
"\n=== pd.concat([df1, df2], axis=1) horizontal concatenation ==="
)
(
result
)
Expected output:
=== 原始 DataFrame ===
df1:
name age
0 Alice 25
1 Bob 30
2 Charlie 35
df2:
city score
0 Beijing 85
1 Shanghai 90
2 Guangzhou 95
=== pd.concat([df1, df2], axis=1) 横向拼接 ===
name age city score
0 Alice 25 Beijing 85
1 Bob 30 Shanghai 90
2 Charlie 35 Guangzhou 95
Code analysis:
- Use
axis=1Concatenate two DataFrames by column. - Horizontal concatenation requires both DataFrames to have the same number of rows (index alignment).
- The concatenated DataFrame contains all columns from both objects.
Example 3: Use the keys parameter to create a hierarchical index
keys The parameter helps identify the data source, which is very useful when merging content from different datasets.
Example
import
pandas
as
pd
1. Create a dictionary containing multiple Series
data
=
{
'group_a'
: pd.
Series
(
[
1
,
2
,
3
,
4
]
)
,
'group_b'
: pd.
Series
(
[
5
,
6
,
7
,
8
]
)
,
'group_c'
: pd.
Series
(
[
9
,
10
,
11
,
12
]
)
}
(
"=== Concatenate using the keys parameter ==="
)
2. Use the keys parameter to create a hierarchical index
result
=
pd.
concat
(
data
,
keys
=
[
'Group 1'
,
'Group 2'
,
'third group'
]
)
(
result
)
3. Access data at a specific level
(
"\n=== Access data of 'first group' ==="
)
(
result
[
'Group 1'
]
)
Expected output:
=== 使用 keys 参数拼接 ===
第一组 group_a 1
group_b 2
group_c 3
group_a 4
第二组 group_a 5
group_b 6
group_c 7
group_a 8
第二组 group_b 9
...
Code analysis:
keysThe parameter creates a multi-level (hierarchical) index for the concatenation result, with the first level identifying the group of the data source.- can be used via
result['第一组']Access data of a specific group.
Example 4: Handling DataFrames with different columns (join parameter)
When the DataFrames to concatenate have different columns, you can use the join parameter to control how they are handled.
Example
import
pandas
as
pd
1. Create DataFrames with different columns
df1
=
pd.
DataFrame
(
{
'name'
:
[
'Alice'
,
'Bob'
]
,
'age'
:
[
25
,
30
]
}
)
df2
=
pd.
DataFrame
(
{
'name'
:
[
'Charlie'
,
'Diana'
]
,
'score'
:
[
85
,
90
]
}
)
(
"=== Original DataFrame ==="
)
(
"df1:"
)
(
df1
)
(
"\ndf2:"
)
(
df2
)
2. Outer join (default) - keep all columns
(
"\n=== join='outer' (outer join) ==="
)
(
pd.
concat
(
[
df1
,
df2
]
,
join
=
'outer'
)
)
3. Inner join - keep only common columns
(
"\n=== join='inner' (inner join) ==="
)
(
pd.
concat
(
[
df1
,
df2
]
,
join
=
'inner'
)
)
Expected output:
=== 原始 DataFrame ===
df1:
name age
0 Alice 25
1 Bob 30
df2:
name score
0 Charlie 85
1 Diana 90
=== join='outer' (外连接) ===
name age score
0 Alice 25.0 NaN
1 Bob 30.0 NaN
0 Charlie NaN 85.0
1 Diana NaN 90.0
=== join='inner' (内连接) ===
name
0 Alice
1 Bob
0 Charlie
1 Diana
Code analysis:
join='outer'(default) keeps all columns, with missing columns filled withNaN。join='inner'keeps only the columns common to both DataFrames (here,name)。
Tip:
pd.concat()Suitable for simple concatenation. For more complex merges (such as matching based on column values), seepd.merge()function in Vue 3.
Common Pandas functions