w3resource

NumPy: Add a new row to an empty numpy array

NumPy: Array Object Exercise-119 with Solution

Write a NumPy program to add a new row to an empty numpy array.

Sample Solution:

Python Code:

import numpy as np    
arr = np.empty((0,3), int)
print("Empty array:")
print(arr)
arr = np.append(arr, np.array([[10,20,30]]), axis=0)
arr = np.append(arr, np.array([[40,50,60]]), axis=0)
print("After adding two new arrays:")
print(arr)

Sample Output:

Empty array:
[]
After adding two new arrays:
[[10 20 30]
 [40 50 60]]

Pictorial Presentation:

Python NumPy: Find the position of the index of a specified value greater than existing value in numpy array

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to find the position of the index of a specified value greater than existing value in numpy array.
Next: Write a NumPy program to get the index of a maximum element in a numpy array along one axis.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

List comprehension:

>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]