Built with Tutify.io

    AIStudyLabs

    Pandas Stack Two Series Vertically and Horizontally

    Pandas Stack Two Series Vertically and Horizontally

    pandas.concat() function is used to stack two given series vertically and horizontally in pandas. When you concat() two pandas Series along with row-wise, it creates a new Series where the elements are all rows of two Series, basically it does append one Series with another. In this article, I will explain how to stack two pandas series vertically and horizontally with examples.

    Key Points –

    <a id="h-quick-examples-of-stack-two-series-vertically-and-horizontally"></a>

    Quick Examples of Stack Two Series Vertically and Horizontally

    If you are in a hurry, below are some quick examples of how to stack two pandas Series vertically and horizontally.

    
    # Quick examples of stack two series vertically and horizontally
    
    # Example 1: Stack two series horizontally 
    # Using pandas.concat() function
    ser2 = pd.concat([ser, ser1], axis = 1)
    
    # Example 2: Stack two series vertically
    # Using pandas.concat() function
    ser2 = pd.concat([ser, ser1], axis = 0)
    
    # Example 3: Stack two series vertically 
    # Using Series.append() function
    ser2 = ser.append(ser1)
    
    # Example 4: Use pandas.concat() & ignore_index=True
    ser2 = pd.concat([ser, ser1], ignore_index=True)
    

    <a id="h-create-two-pandas-series"></a>

    Create Two Pandas Series

    Pandas Series is a one-dimensional, Index-labeled data structure available only in the Pandas library. It can store all the datatypes such as strings, integers, float, and other python objects. We can access each element in the Series with the help of corresponding default indices.

    Now, let’s create two pandas series using a list of values.

    
    import pandas as pd
      
    # Create two pandas series
    ser = pd.Series(['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"])
    ser1 = pd.Series([20000,25000,23000,28000,55000,23000,28000])
    
    print(ser)  
    print(ser1)
    

    Yields below output.

    
    # Output:
    0       Java
    1      Spark
    2    PySpark
    3     Pandas
    4      NumPy
    5     Python
    6     Oracle
    dtype: object
    0    20000
    1    25000
    2    23000
    3    28000
    4    55000
    5    23000
    6    28000
    dtype: int64
    

    <a id="h-stack-two-pandas-series-horizontally"></a>

    Stack Two Pandas Series Horizontally

    pandas.concat() function is used to stack two pandas Series horizontally. For that, we need to pass axis=1 along with a list of series. If you wanted to concatenate two pandas DataFrame columns refer pandas.concate()function.

    
    # Stack two series horizontally 
    # Using pandas.concat() function
    ser2 = pd.concat([ser, ser1], axis = 1)
    print(ser2)
    

    Yields below output.

    
    # Output:
             0      1
    0     Java  20000
    1    Spark  25000
    2  PySpark  23000
    3   Pandas  28000
    4    NumPy  55000
    5   Python  23000
    6   Oracle  28000
    

    <a id="h-stack-two-pandas-series-vertically"></a>

    Stack Two Pandas Series Vertically

    Alternatively, you can also stack two pandas Series vertically by passing axis=0 parameter into pandas.concat() function.

    
    # Stack two series vertically using pandas.concat() function
    ser2 = pd.concat([ser, ser1], axis = 0)
    print(ser2)
    

    Yields below output.

    
    # Output:
    0       Java
    1      Spark
    2    PySpark
    3     Pandas
    4      NumPy
    5     Python
    6     Oracle
    0      20000
    1      25000
    2      23000
    3      28000
    4      55000
    5      23000
    6      28000
    dtype: object
    

    <a id="h-stack-two-series-vertically-using-series-append-function"></a>

    Stack Two Series Vertically Using Series.append() Function

    Similarly, you can also use Series.append() function to stack two series vertically. For E.x, ser.append(ser1) appends ser1 to the ser Series. If you want to append two pandas DataFrame refer to append()function.

    
    # Stack two series append() vertically 
    ser2 = ser.append(ser1)
    print(ser2)
    

    Yields the same output as above.

    <a id="h-use-pandas-concat-amp-ignore-index-true"></a>

    Use pandas.concat() & ignore_index=True

    Using pandas.concat() with ignore_index=True to reset the index on the combined Series. For instance, pd.concat() vertically stacks ser and ser1, and the resulting Series ser2 has a new continuous index starting from 0, ignoring the original indices of the input Series.

    
    # Use pandas.concat() & ignore_index=True
    ser2 = pd.concat([ser, ser1], ignore_index=True)
    print(ser2)
    

    Yields below output.

    
    # Output:
    0        Java
    1       Spark
    2     PySpark
    3      Pandas
    4       NumPy
    5      Python
    6      Oracle
    7       20000
    8       25000
    9       23000
    10      28000
    11      55000
    12      23000
    13      28000
    dtype: object
    

    <a id="h-complete-examples"></a>

    Complete Examples

    
    import pandas as pd
    
    # Create the Series
    ser = pd.Series(['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"])
    ser1 = pd.Series([20000,25000,23000,28000,55000,23000,28000])
    print(ser)  
    print(ser1)
    
    # Stack two series horizontally 
    # Using pandas.concat() function
    ser2 = pd.concat([ser, ser1], axis = 1)
    print(ser2)
    
    # Stack two series vertically 
    # Using pandas.concat() function
    ser2 = pd.concat([ser, ser1], axis = 0)
    print(ser2)
    
    # Stack two series vertically 
    # Using Series.append() function
    ser2 = ser.append(ser1)
    print(ser2)
    
    # Use pandas.concat() & ignore_index=True
    ser2 = pd.concat([ser, ser1], ignore_index=True)
    print(ser2)
    

    Frequently Asked Questions on Stack Two Series Vertically and Horizontally

    To stack two Series vertically means combining them into a single Series with one stacked on top of the other. This operation increases the length of the resulting Series.

    You can stack two Series vertically using the pd.concat() function from the Pandas library. By default, pd.concat() concatenates along the vertical axis (axis=0).

    When you set ignore_index=True with pd.concat(), it creates a new index for the resulting Series, ignoring the original indices of the input Series. This is useful when you want a continuous index for the stacked Series.

    To stack two Series horizontally, you can use pd.concat() with the axis=1 parameter. This concatenates the Series side by side, increasing the width of the resulting DataFrame.

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

    Conclusion

    In this article, I have explained how to stack two given series vertically and horizontally in pandas using pandas.concat(), Series.append() functions with examples.

    Happy Learning !!

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

    Related Articles

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

    References