Geek Slack

Learn Numerical Python
    About Lesson


    NumPy Joining Arrays


    NumPy Joining Arrays

    Joining arrays in NumPy allows you to combine multiple arrays into a single array. This is useful for various data manipulation tasks. NumPy provides several functions to join arrays.

    Concatenating Arrays

    The numpy.concatenate() function joins a sequence of arrays along an existing axis.

    Example: Concatenating 1-D Arrays

    import numpy as np
    
    # Creating two 1-D arrays
    arr1 = np.array([1, 2, 3])
    arr2 = np.array([4, 5, 6])
    
    # Concatenating the arrays
    result = np.concatenate((arr1, arr2))
    print(result)  # Output: [1 2 3 4 5 6]

    Example: Concatenating 2-D Arrays

    import numpy as np
    
    # Creating two 2-D arrays
    arr1 = np.array([[1, 2], [3, 4]])
    arr2 = np.array([[5, 6], [7, 8]])
    
    # Concatenating the arrays along axis 0
    result = np.concatenate((arr1, arr2), axis=0)
    print(result)
    # Output:
    # [[1 2]
    #  [3 4]
    #  [5 6]
    #  [7 8]]
    
    # Concatenating the arrays along axis 1
    result = np.concatenate((arr1, arr2), axis=1)
    print(result)
    # Output:
    # [[1 2 5 6]
    #  [3 4 7 8]]

    Stacking Arrays

    The numpy.stack() function joins a sequence of arrays along a new axis.

    Example: Stacking 1-D Arrays

    import numpy as np
    
    # Creating two 1-D arrays
    arr1 = np.array([1, 2, 3])
    arr2 = np.array([4, 5, 6])
    
    # Stacking the arrays along a new axis
    result = np.stack((arr1, arr2), axis=0)
    print(result)
    # Output:
    # [[1 2 3]
    #  [4 5 6]]
    
    result = np.stack((arr1, arr2), axis=1)
    print(result)
    # Output:
    # [[1 4]
    #  [2 5]
    #  [3 6]]

    Stacking Along Rows and Columns

    NumPy provides helper functions like hstack() and vstack() for stacking along rows and columns, respectively.

    Example: Horizontal Stacking

    import numpy as np
    
    # Creating two 1-D arrays
    arr1 = np.array([1, 2, 3])
    arr2 = np.array([4, 5, 6])
    
    # Horizontal stacking
    result = np.hstack((arr1, arr2))
    print(result)  # Output: [1 2 3 4 5 6]

    Example: Vertical Stacking

    import numpy as np
    
    # Creating two 1-D arrays
    arr1 = np.array([1, 2, 3])
    arr2 = np.array([4, 5, 6])
    
    # Vertical stacking
    result = np.vstack((arr1, arr2))
    print(result)
    # Output:
    # [[1 2 3]
    #  [4 5 6]]

    Depth Stacking

    The dstack() function stacks arrays along the third dimension.

    Example: Depth Stacking

    import numpy as np
    
    # Creating two 1-D arrays
    arr1 = np.array([1, 2, 3])
    arr2 = np.array([4, 5, 6])
    
    # Depth stacking
    result = np.dstack((arr1, arr2))
    print(result)
    # Output:
    # [[[1 4]
    #   [2 5]
    #   [3 6]]]

    Joining Arrays with append()

    You can also use the numpy.append() function to join arrays.

    Example: Using append()

    import numpy as np
    
    # Creating two 1-D arrays
    arr1 = np.array([1, 2, 3])
    arr2 = np.array([4, 5, 6])
    
    # Using append to join arrays
    result = np.append(arr1, arr2)
    print(result)  # Output: [1 2 3 4 5 6]

    Conclusion

    Joining arrays in NumPy is an essential operation for data manipulation. Whether you’re concatenating, stacking, or appending arrays, NumPy provides a variety of functions to efficiently combine arrays.