Python | Ways to add row/columns in numpy array
Given numpy array, the task is to add rows/columns basis on requirements to numpy array. Let’s see a few examples of this problem.
Method #1: Using np.hstack() method
# Python code to demonstrate # adding columns in numpy array import numpy as np ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]]) # printing initial array print("initial_array : ", str(ini_array)); # Array to be added as column column_to_be_added = np.array([1, 2, 3]) # Adding column to numpy array result = np.hstack((ini_array, np.atleast_2d(column_to_be_added).T)) # printing result print ("resultant array", str(result)) |
chevron_right
filter_none
Output:
initial_array : [[ 1 2 3] [45 4 7] [ 9 6 10]] resultant array [[ 1 2 3 1] [45 4 7 2] [ 9 6 10 3]]
Method #2: Using column_stack() method
# python code to demonstrate # adding columns in numpy array import numpy as np ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]]) # printing initial array print("initial_array : ", str(ini_array)); # Array to be added as column column_to_be_added = np.array([1, 2, 3]) # Adding column to numpy array result = np.column_stack((ini_array, column_to_be_added)) # printing result print ("resultant array", str(result)) |
chevron_right
filter_none
Output:
initial_array : [[ 1 2 3] [45 4 7] [ 9 6 10]] resultant array [[ 1 2 3 1] [45 4 7 2] [ 9 6 10 3]]
Method #3: Using np.vstack() method
# python code to demonstrate # adding rows in numpy array import numpy as np ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]]) # printing initial array print("initial_array : ", str(ini_array)); # Array to be added as row row_to_be_added = np.array([1, 2, 3]) # Adding row to numpy array result = np.vstack ((ini_array, row_to_be_added) ) # printing result print ("resultant array", str(result)) |
chevron_right
filter_none
Output:
initial_array : [[ 1 2 3] [45 4 7] [ 9 6 10]] resultant array [[ 1 2 3] [45 4 7] [ 9 6 10] [ 1 2 3]]
Recommended Posts:
- Python | Ways to convert array of strings to array of floats
- Python | Flatten a 2d numpy array into 1d array
- Python | Multiply 2d numpy array corresponding to 1d array
- Python | Reverse a numpy array
- Python | Find Mean of a List of Numpy Array
- Python | Replace negative value with zero in numpy array
- Python | Filter out integers from float numpy array
- Python | dtype object length of Numpy array of strings
- Python | Ways to split a string in different ways
- Python | Numpy numpy.ndarray.__pos__()
- Python | Numpy numpy.ndarray.__truediv__()
- Python | Numpy numpy.ndarray.__neg__()
- Python | Numpy numpy.ndarray.__invert__()
- Python | Numpy numpy.ndarray.__le__()
- Python | Numpy numpy.ndarray.__eq__()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



