Built with Tutify.io

    AIStudyLabs

    Pandas Series concat() Function

    Pandas Series concat() Function

    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 –

    <a id="h-syntax-of-pandas-series-concat-function"></a>

    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)
    

    <a id="h-parameters-of-the-nbsp-series-concat"></a>

    Parameters of the Series concat()

    Following are the parameters of the Series concat() function.

    <a id="h-return-value"></a>

    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.

    <a id="h-concatenate-pandas-series"></a>

    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.

    <a id="h-concatenate-pandas-series-along-with-axis-1"></a>

    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
    

    <a id="h-concatenate-multiple-pandas-series"></a>

    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
    

    <a id="h-concatenate-multiple-series-along-with-axis-1"></a>

    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.

    <a id="h-concatenate-series-with-duplicate-indexes"></a>

    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.

    <a id="h-concatenate-series-with-hierarchical-index"></a>

    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.

    <a id="h-frequently-asked-questions-on-nbsp-pandas-series-concat-function"></a>

    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.

    <a id="h-conclusion"></a>

    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!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References