![]() |
|
[Tut] How to Extend a NumPy Array in Python - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Extend a NumPy Array in Python (/thread-99566.html) |
[Tut] How to Extend a NumPy Array in Python - xSicKxBot - 06-13-2022 How to Extend a NumPy Array in Python Rate this post Summary: Call the append function of the Numpy library as: Code: numpy.append(given_array, elements_to_be_appended, axis)Other ways of extending the array include using: (i) the Code: vstackCode: column_stackCode: numpy.insert[b]Problem Formulation[/b] Given a Numpy array; How will you extend the given array with values along rows and columns? Example: Consider the following array – Code: import numpy as np arr = np.array([[1, 2], [3, 4]])
print(arr)Question: How will you add an extra row and column to the array such that the expected output is: Code: [[1 2 7] [3 4 8] [5 6 9]]Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.) ![]() [b]Method 1: Using numpy.append()[/b]
Explanation:
Code: Code: import numpy as np arr = np.array([[1, 2], [3, 4]])
# add elements row-wise
arr = np.append(arr, [[5, 6]], 0)
# add elements column-wise
arr = np.append(arr, [[7], [8], [9]], 1)
print(arr)Output: Code: [[1 2 7] [3 4 8] [5 6 9]]
Note:
Code: Code: import numpy as np arr = np.array([[1, 2], [3, 4]])
# add elements row-wise
arr = np.vstack([arr, [5, 6]])
# add elements column-wise
arr = np.column_stack([arr, [7, 8, 9]])
print(arr)Output: Code: [[1 2 7] [3 4 8] [5 6 9]]The Code: numpy.insert()Call the Code: np.insert()Code: axis=0Code: axis=1Code: import numpy as np arr = np.array([[1, 2], [3, 4]])
# add elements row-wise (insert before row 2)
arr = np.insert(arr, 2, values=[5, 6], axis=0)
# add elements column-wise (insert before column 2)
arr = np.insert(arr, 2, values=[7, 8, 9], axis=1)
print(arr)Explanation:
[b]Method 4: Concatenate Two 2D Arrays[/b] Note: NumPy’s Code: concatenate()Code: np.concatenate(a, b, axis=0)Code: np.concatenate(a, b, axis=None)
Code: import numpy as np arr_a = np.array([[1, 2], [3, 4]])
arr_b = np.array([[5, 6], [7, 8]])
print('merge across columns: ')
arr = np.concatenate((arr_a, arr_b), axis=1)
print(arr)
print('merge across rows: ')
arr = np.concatenate((arr_a, arr_b), axis=0)
print(arr)Output: Code: merge across columns: [[1 2 5 6] [3 4 7 8]]
merge across rows: [[1 2] [3 4] [5 6] [7 8]]There are other ways of merging two given arrays which include approaches that we already learned above. To explore more on this feel free to read the following tutorial: How to Concatenate Two NumPy Arrays? [b]Conclusion[/b] We have learned as many as four ways of extending a given array in this article. Feel free to use the option that suits your requirements. I hope this article helped you. Please subscribe and stay tuned for more interesting tutorials and discussions. Recommended Tutorials:
Web Scraping with BeautifulSoup ![]() One of the most sought-after skills on Fiverr and Upwork is web scraping . Make no mistake: extracting data programmatically from websites is a critical life-skill in today’s world that’s shaped by the web and remote work. This course teaches you the ins and outs of Python’s BeautifulSoup library for web scraping. |