In pandas, you can concatenate two or more Series using the concat()
function. This function is used to concatenate Pandas objects along a specified axis. In this article, I will explain the Pandas series concat() function and using its syntax, parameters, and usage how we can concatenate two or more pandas objects, such as Series, along a specified axis with examples.
Key Points –
- The
concat()
function in pandas is used to concatenate pandas objects, such as Series, along a specified axis, either vertically (axis 0) or horizontally (axis 1). - When concatenating Series with duplicate indexes, the
ignore_index
parameter can be used to reset the index of the resulting object. - The
keys
parameter inconcat()
allows you to create a hierarchical index, distinguishing between the original Series when concatenating multiple objects. - The function provides various parameters such as
axis
,ignore_index
,keys
,join
, and others, offering flexibility in customizing the concatenation operation. - The function provides options for index handling, including resetting the index with
ignore_index
and creating hierarchical indexes with thekeys
parameter, allowing users to control the resulting structure of the concatenated data.
Syntax of Pandas Series concat() Function
Following is the syntax of the pandas series concat() function.
# Syntax of Series concat() function
pd.concat(objs, axis=0, join='outer', ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=False, copy=True)
Parameters of the Series concat()
Following are the parameters of the Series concat() function.
objs
– This is the list or dict of pandas objects (typically Series) that you want to concatenate.axis
– Specifies the axis along which the concatenation will be performed. Use axis=0 for vertical concatenation (along rows) and axis=1 for horizontal concatenation (along columns).join
– Determines how to handle overlapping indexes. It can be ‘outer’ (default) or ‘inner’.ignore_index
– If set to True, it resets the index of the resulting object.keys
– If specified, it creates a hierarchical index using the provided keys.levels
,names
– These parameters are used if keys are provided for creating a hierarchical index.sort
– If set to True, it sorts the resulting object’s index.copy
– If set to False, it avoids copying data when possible.
Return Value
The pd.concat()
function returns a new pandas object, which could be a Series or a DataFrame depending on the input objects and the concatenation axis. The return type is determined by the types of objects being concatenated.
Let’s create two Pandas Series.
import pandas as pd
# Create two Series
series1 = pd.Series([2, 4, 6], name='Series1')
print("First Series:\n", series1)
series2 = pd.Series([8, 10, 12], name='Series2')
print("Secomd Series:\n", series2)
Yields below output.
Concatenate Pandas Series
To concatenate the Pandas Series you can use the pd.concat()
function. By default, this function concatenates the Series along with the axis 0 which means both Series elements are merged in the new Series vertically. For instance, Pass both the Series into pd.concat() function, it will concatenate the given Series and return the new Series.
import pandas as pd
# Create two Series
series1 = pd.Series([2, 4, 6], name='Series1')
series2 = pd.Series([8, 10, 12], name='Series2')
# Concatenate the two Series
result = pd.concat([series1, series2])
print("Concatenate the two Series:\n",result)
As you can see, the resulting Series has a new index, and you can see that the elements from both Series are combined into a single Series. This example yields the below output.
If you want to reset the index of the resulting Series after concatenation, you can use the ignore_index
parameter of the pd.concat()
function. Setting ignore_index
to True
will reset the index, and the resulting Series will have a new index based on the concatenation order.
# Concatenate the two Series using ignore_index
result = pd.concat([series1, series2], ignore_index=True)
print("Ignoring indexes during concatenation:\n",result)
# Output:
# Ignoring indexes during concatenation:
# 0 2
# 1 4
# 2 6
# 3 8
# 4 10
# 5 12
# dtype: int64
In this case, the resulting Series has a new index starting from 0, and it does not retain the original indices from series1
and series2
.
Concatenate Pandas Series along with Axis = 1
Concatenating two pandas Series along with the axis 1
involves stacking them horizontally. You can achieve this using the pd.concat()
function with the axis
parameter set to 1.
# Concatenate along axix=1
result = pd.concat([series1, series2], axis=1)
print("Concatenate the Strings along with axis = 1:\n",result)
# Output:
# Concatenate along with axis = 1:
# Series1 Series2
# 0 2 8
# 1 4 10
# 2 6 12
In the above example, pd.concat([series1, series2], axis=1)
concatenates series1
and series2
based on axis = 1, resulting in a DataFrame. Each original Series becomes a separate column in the DataFrame.
If you want to reset the index of the resulting DataFrame, you can use the ignore_index
parameter. Now, the resulting DataFrame has a new index, and the columns are labeled with integers starting from 0.
# Concatenate the Series using axis = 1, ignore_index=True
result = pd.concat([series1, series2], axis=1, ignore_index=True)
print(result)
# Output:
# 0 1
# 0 2 8
# 1 4 10
# 2 6 12
Concatenate Multiple Pandas Series
Alternatively, you can concatenate more than two pandas Series using the concat() function. For instance, create three Series and pass them into this function, it will concatenate all Series elements vertically and form a new Series. The resulting Series has a new index.
import pandas as pd
# Create three Series
series1 = pd.Series([2, 4, 6], name='Series1')
series2 = pd.Series([8, 10, 12], name='Series2')
series3 = pd.Series([14, 16, 18], name='Series3')
# Concatenate multiple Series long axis 0
result = pd.concat([series1, series2, series3])
print("Concatenate multiple Series along axis 0:\n",result)
# Output:
# Concatenate multiple Series along axis 0:
# 0 2
# 1 4
# 2 6
# 0 8
# 1 10
# 2 12
# 0 14
# 1 16
# 2 18
# dtype: int64
Concatenate Multiple Series Along with Axis = 1
Similarly, you can concatenate more than two pandas Series horizontally using the concat() function. For that, you need to set the axis
param of the concat()
function is 1
and pass it into this function along with the specified number of Series. It will stack them horizontally and form a new Series.
# Concatenate multiple Series along axis 1
result = pd.concat([series1, series2, series3], axis=1)
print("Concatenate multiple Series along axis 1:\n",result)
# Output:
# Concatenate multiple Series along axis 1:
# Series1 Series2 Series3
# 0 2 8 14
# 1 4 10 16
# 2 6 12 18
In the above example, pd.concat([series1, series2, series3], axis=1)
concatenates the three Series along axis 1, creating a DataFrame. Each original Series becomes a separate column in the DataFrame.
Concatenate Series with Duplicate Indexes
When concatenating pandas Series with duplicate indexes, you need to be cautious about potential conflicts. If the indexes are not unique, you may encounter issues. However, you can handle this situation by using the ignore_index
parameter or by concatenating along a new level of the index.
import pandas as pd
# Create two Series with duplicate indexes
series1 = pd.Series([2, 4, 6], name='Series1', index=[0, 1, 2])
series2 = pd.Series([8, 10, 12], name='Series2', index=[2, 3, 4])
# Concatenate and ignore indexes
result = pd.concat([series1, series2], ignore_index=True)
print("Concatenation with ignore_index:\n",result)
# Output:
# Concatenation with ignore_index:
# 0 2
# 1 4
# 2 6
# 3 8
# 4 10
# 5 12
# dtype: int64
In the approach (ignore_index=True
), the resulting Series has a new index, and the original indices are ignored.
Concatenate Series with Hierarchical Index
Concatenating pandas Series with hierarchical indexes can be done by using the keys
parameter in the pd.concat()
function.
import pandas as pd
# Create two Series
series1 = pd.Series([2, 4, 6], name='Series1')
series2 = pd.Series([8, 10, 12], name='Series2')
# Concatenate with hierarchical indexes
result = pd.concat([series1, series2], keys=['First', 'Second'])
print("Concatenate with hierarchical indexes:\n",result)
# Output:
# Concatenate with hierarchical indexes:
# First 0 2
# 1 4
# 2 6
# Second 0 8
# 1 10
# 2 12
# dtype: int64
In the above example, pd.concat([series1, series2], keys=['First', 'Second'])
concatenates the two Series and creates a hierarchical index. The keys
parameter is used to label each level of the index.
Frequently Asked Questions on Pandas Series concat() Function
The concat()
function in pandas is used to concatenate two or more pandas objects, such as Series or DataFrames, along a specified axis. It allows you to combine data along rows or columns, depending on the axis parameter.
To concatenate two Series along axis 0 (stack them vertically), you can use the pd.concat()
function without specifying the axis
parameter, as axis 0 is the default.
To concatenate two pandas Series along axis 1 (i.e., stack them horizontally as columns), you can use the pd.concat()
function with the axis
parameter set to 1.
The ignore_index
parameter in the pd.concat()
function is used to reset the index of the resulting object after concatenation. When ignore_index
is set to True
, it discards the existing indices of the concatenated objects and replaces them with a new default integer index based on the concatenation order.
To concatenate pandas Series with different names, you can use the pd.concat()
function with the keys
parameter. The keys
parameter allows you to create a hierarchical index, distinguishing between the different Series.
If Series have duplicate indexes, you can handle the situation by using the ignore_index
parameter or by concatenating along a new level of the index.
Conclusion
In this article, I have explained the pd.concat()
function serves as a versatile tool for seamlessly merging Series along specified axes. By understanding its syntax, parameters, and usage, users can confidently manipulate and concatenate data with precision. Whether handling duplicate indexes, creating hierarchical structures, or customizing concatenation operations, the concat() function empowers data scientists and analysts with a powerful tool for efficient data manipulation in Python.
Happy Learning!!
Related Articles
- Pandas Iterate Over Series
- Pandas Series.isin() Function
- Convert Pandas Series to String
- How to Rename a Pandas Series
- Pandas Series.max() Function
- Pandas.Series.combine() Function
- Pandas Series.mean() Function
- Pandas Series.max() Function
- Pandas Get Floor or Ceil of Series
- Convert Pandas Series to DataFrame
- Pandas Series astype() Function
- Pandas Series sum() Function
- Pandas Series map() Function
- Pandas Series unique() Function with Examples
- How to Get the Length of a Series in Pandas?
- Pandas Series groupby() Function with Examples