To add elements to your dataset, you can use np.append
Syntax
np.append(ndarray, elements you want to add, axis)
axis = 0
is row (will be added to the bottom most row)
axis = 1
is column (will be added to right most column)
Let's start with an array called x and add an integer to the end of the array
To add more than 1 element, put your elements in a list and append that list to the array
Let's start with a new array y
.
To add extra row of elements to y
, specify axis = 0
to np.append
To add extra column of elements to y
, specify axis = 1
.
Note that the syntax between adding rows and columns differs slightly. To add elements to columns, you need to add each element between its own square bracket.
Use this array for the following practice:
myArray = np.array([[11,12,13], [14,15,16], [17,18,19]])
1. Add a new row of elements containing 20, 21 and 22
2. Add a new column of elements containing 30, 40 and 50
You are expected to comply with University policies and guidelines namely, Appropriate Use of Information Resources Policy, IT Usage Policy and Social Media Policy. Users will be personally liable for any infringement of Copyright and Licensing laws. Unless otherwise stated, all guide content is licensed by CC BY-NC 4.0.