Looking for a Tutor Near You?

Post Learning Requirement »
x

Choose Country Code

x

Direction

x

Ask a Question

x

x
x
x
Hire a Tutor

Note On Data Science With Python Part 2

Loading...

After Slicing come Sometimes you need to change the values of particular elements in the array.

Prantik S / Kolkata

11 years of teaching experience

Qualification: MCA (Jaipur National University - [JNU], Jaipur - 2017)

Teaches: Basic Computer, Computer for official job, MS Office, School Level Computer, ICT Training, Computer Science, Information Practice, IT & Computer Subjects, BCA Tuition, IT, Computer, C / C++, C# (C Sharp), Java And J2EE, Python Programming, Visual Basic, BCA Subjects, Hardware Training, Networking, Java Script

Contact this Tutor
  1. Assigning Single Values Sometimes you need to change the values of particular elements in the array. For example, we noticed the fourth entry in the heights_arr was incorrect, it should be 165 instead of 163, we can re-assign the correct number by: heights_arr[3] = 165 Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] heights_arr = np.array(heights) heights_arr[3] = 165 print(heights_arr) In a 2darray, single values can be assigned easily. You can use indexing for one element. For example, change the fourth entry in heights_arr to 165: heights_and_ages_arr[0, 3] = 165 heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) heights_and_ages_arr[0, 3] = 165 print(heights_and_ages_arr) Or we can use slicing for multiple elements. For example, to replace the first row by its mean 180 in heights_and_ages_arr: heights_and_ages_arr[0,:] = 180 heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 54, 47, 70] 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46,
  2. heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) heights_and_ages_arr[O,:] = 180 print(heights_and_ages_arr) We can also combine slicing to change any subset of the array. For example, to reassign 0 to the left upper corner: heights_and_ages_arr[:2, :2] = 0 heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) heights_and_ages_arr[:2, :2] = 0 print(heights_and_ages_arr) It is easy to update values in a subarray when you combine arrays with slicing. For more on basic slicing and advanced indexing in numpy. Assigning an Array to an Array In addition, a Idarray or a 2darry can be assigned to a subset of another 2darray, as long as their shapes match. Recall the 2darray heights_and_ages_arr: heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights _ and heights _ and heights _ and print(heights ages = heights + ages ages_arr = np.array(heights_and_ages) ages_arr = heights_and_ages_arr.reshape((2,45)) and_ages_arr)
  3. If we want to update both height and age of the first president with new data, we can supply the data in a list: heights_and_ages_arr[:, 0] = [190, 58] heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) heights_and_ages_arr[:, 0] = [190, 58] print(heights_and_ages_arr) We can also update data in a subarray with a numpy array as such: new record = 183, 190], [54, 50, 69]]) 42:] = new_record heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) new record = np.array([[180, 183, 190], [54, 50, 69]]) 42:] = new_record print(heights_and_ages_arr) Note the last three columns' values have changed. Updating a multidimensional array with a new record is straightforward in numpy as long as their shapes match.
  4. Assigning Single Values Sometimes you need to change the values of particular elements in the array. For example, we noticed the fourth entry in the heights_arr was incorrect, it should be 165 instead of 163, we can re-assign the correct number by: heights_arr[3] = 165 Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] heights_arr = np.array(heights) heights_arr[3] = 165 print(heights_arr) In a 2darray, single values can be assigned easily. You can use indexing for one element. For example, change the fourth entry in heights_arr to 165: heights_and_ages_arr[0, 3] = 165 heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) heights_and_ages_arr[0, 3] = 165 print(heights_and_ages_arr) Or we can use slicing for multiple elements. For example, to replace the first row by its mean 180 in heights_and_ages_arr: heights_and_ages_arr[0,:] = 180 heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 54, 47, 70] 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46,
  5. Combining Two Arrays Oftentime we obtain data stored in different arrays and we need to combine them into one to keep it in one place. For example, instead of having the ages stored in a list, it could be stored in a 2darray: ages_arr.shape ages_arr[:3,] Try it Yourself import numpy as np ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] ages_arr = np.array(ages) print(ages_arr.shape) print(ages_arr[:3,]) If we reshape the heights_arr to (45,1), the same as lages_arrl, we can stack them horizontally (by column) to get a 2darray using lhstackl: heights_arr = heights_arr.reshape((45,1)) height_age_arr = np.hstack((heights_arr, ages_arr)) height_age_arr.shape height_age_arr[:3,] Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_arr = np.array(heights) ages_arr = np.array(ages) heights_arr = heights_arr.reshape((45,1)) ages_arr = ages_arr.reshape((45,1)) height_age_arr = np.hstack((heights_arr, ages_arr)) print(height_age_arr.shape) print(height_age_arr[:3,]) Now height_age_arr has both heights and ages for the presidents, each column corresponds to the height and age of one president. Similarly, if we want to combine the arrays vertically (by row), we can use Ivstackl. heights_arr = heights_arr.reshape((1,45)) ages_arr = ages_arr.reshape((1,45))
  6. heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) heights_and_ages_arr[O,:] = 180 print(heights_and_ages_arr) We can also combine slicing to change any subset of the array. For example, to reassign 0 to the left upper corner: heights_and_ages_arr[:2, :2] = 0 heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) heights_and_ages_arr[:2, :2] = 0 print(heights_and_ages_arr) It is easy to update values in a subarray when you combine arrays with slicing. For more on basic slicing and advanced indexing in numpy. Assigning an Array to an Array In addition, a Idarray or a 2darry can be assigned to a subset of another 2darray, as long as their shapes match. Recall the 2darray heights_and_ages_arr: heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights _ and heights _ and heights _ and print(heights ages = heights + ages ages_arr = np.array(heights_and_ages) ages_arr = heights_and_ages_arr.reshape((2,45)) and_ages_arr)
  7. height_age_arr = np.vstack((heights_arr, ages_arr)) height_age_arr.shape height_age_arr[:,:3] Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_arr = np.array(heights) ages_arr = np.array(ages) heights_arr = heights_arr.reshape((1,45)) ages_arr = ages_arr.reshape((1,45)) height_age_arr = np.vstack((heights_arr, ages_arr)) print(height_age_arr.shape) print(height_age_arr[:,:3]) To combine more than two arrays horizontally, simply add the additional arrays into the tuple. Concatenate More generally, we can use the function numpy.concatenate. If we want to concatenate, link together, two arrays along rows, then pass 'axis = Il to achieve the same result as using numpy.hstack; and pass 'axis = 01 if you want to combine arrays vertically. In the example from the previous part, we were using hstack to combine two arrays horizontally, instead: height_age_arr = np.concatenate((heights_arr, ages_arr), axis=l) Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_arr = np.array(heights) ages_arr = np.array(ages) heights_arr = heights_arr.reshape((45,1)) ages_arr = ages_arr.reshape((45,1)) # height_age_arr = np.hstack((heights_arr, ages_arr)) height_age_arr = np.concatenate((heights_arr, ages_arr), axis=l) print(height_age_arr.shape)
  8. If we want to update both height and age of the first president with new data, we can supply the data in a list: heights_and_ages_arr[:, 0] = [190, 58] heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) heights_and_ages_arr[:, 0] = [190, 58] print(heights_and_ages_arr) We can also update data in a subarray with a numpy array as such: new record = 183, 190], [54, 50, 69]]) 42:] = new_record heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) new record = np.array([[180, 183, 190], [54, 50, 69]]) 42:] = new_record print(heights_and_ages_arr) Note the last three columns' values have changed. Updating a multidimensional array with a new record is straightforward in numpy as long as their shapes match.
  9. print(height_age_arr[:3,:]) Also you can get the same result as using vstack: height_age_arr = np.concatenate((heights_arr, ages_arr), axis=0) Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_arr = np.array(heights) ages_arr = np.array(ages) heights_arr = heights_arr.reshape((1,45)) ages_arr = ages_arr.reshape((1,45)) #height_age_arr = np.vstack((heights_arr, ages_arr)) height_age_arr = np.concatenate((heights_arr, ages_arr), axis=O) print(height_age_arr.shape) print(height_age_arr[:,:3]) You can use np.hstack to concatenate arrays ONLY if they have the same number of rows.
  10. Combining Two Arrays Oftentime we obtain data stored in different arrays and we need to combine them into one to keep it in one place. For example, instead of having the ages stored in a list, it could be stored in a 2darray: ages_arr.shape ages_arr[:3,] Try it Yourself import numpy as np ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] ages_arr = np.array(ages) print(ages_arr.shape) print(ages_arr[:3,]) If we reshape the heights_arr to (45,1), the same as lages_arrl, we can stack them horizontally (by column) to get a 2darray using lhstackl: heights_arr = heights_arr.reshape((45,1)) height_age_arr = np.hstack((heights_arr, ages_arr)) height_age_arr.shape height_age_arr[:3,] Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_arr = np.array(heights) ages_arr = np.array(ages) heights_arr = heights_arr.reshape((45,1)) ages_arr = ages_arr.reshape((45,1)) height_age_arr = np.hstack((heights_arr, ages_arr)) print(height_age_arr.shape) print(height_age_arr[:3,]) Now height_age_arr has both heights and ages for the presidents, each column corresponds to the height and age of one president. Similarly, if we want to combine the arrays vertically (by row), we can use Ivstackl. heights_arr = heights_arr.reshape((1,45)) ages_arr = ages_arr.reshape((1,45))
  11. height_age_arr = np.vstack((heights_arr, ages_arr)) height_age_arr.shape height_age_arr[:,:3] Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_arr = np.array(heights) ages_arr = np.array(ages) heights_arr = heights_arr.reshape((1,45)) ages_arr = ages_arr.reshape((1,45)) height_age_arr = np.vstack((heights_arr, ages_arr)) print(height_age_arr.shape) print(height_age_arr[:,:3]) To combine more than two arrays horizontally, simply add the additional arrays into the tuple. Concatenate More generally, we can use the function numpy.concatenate. If we want to concatenate, link together, two arrays along rows, then pass 'axis = Il to achieve the same result as using numpy.hstack; and pass 'axis = 01 if you want to combine arrays vertically. In the example from the previous part, we were using hstack to combine two arrays horizontally, instead: height_age_arr = np.concatenate((heights_arr, ages_arr), axis=l) Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_arr = np.array(heights) ages_arr = np.array(ages) heights_arr = heights_arr.reshape((45,1)) ages_arr = ages_arr.reshape((45,1)) # height_age_arr = np.hstack((heights_arr, ages_arr)) height_age_arr = np.concatenate((heights_arr, ages_arr), axis=l) print(height_age_arr.shape)
  12. print(height_age_arr[:3,:]) Also you can get the same result as using vstack: height_age_arr = np.concatenate((heights_arr, ages_arr), axis=0) Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_arr = np.array(heights) ages_arr = np.array(ages) heights_arr = heights_arr.reshape((1,45)) ages_arr = ages_arr.reshape((1,45)) #height_age_arr = np.vstack((heights_arr, ages_arr)) height_age_arr = np.concatenate((heights_arr, ages_arr), axis=O) print(height_age_arr.shape) print(height_age_arr[:,:3]) You can use np.hstack to concatenate arrays ONLY if they have the same number of rows.
  13. Assigning Single Values Sometimes you need to change the values of particular elements in the array. For example, we noticed the fourth entry in the heights_arr was incorrect, it should be 165 instead of 163, we can re-assign the correct number by: heights_arr[3] = 165 Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] heights_arr = np.array(heights) heights_arr[3] = 165 print(heights_arr) In a 2darray, single values can be assigned easily. You can use indexing for one element. For example, change the fourth entry in heights_arr to 165: heights_and_ages_arr[0, 3] = 165 heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) heights_and_ages_arr[0, 3] = 165 print(heights_and_ages_arr) Or we can use slicing for multiple elements. For example, to replace the first row by its mean 180 in heights_and_ages_arr: heights_and_ages_arr[0,:] = 180 heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 54, 47, 70] 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46,
  14. heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) heights_and_ages_arr[O,:] = 180 print(heights_and_ages_arr) We can also combine slicing to change any subset of the array. For example, to reassign 0 to the left upper corner: heights_and_ages_arr[:2, :2] = 0 heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) heights_and_ages_arr[:2, :2] = 0 print(heights_and_ages_arr) It is easy to update values in a subarray when you combine arrays with slicing. For more on basic slicing and advanced indexing in numpy. Assigning an Array to an Array In addition, a Idarray or a 2darry can be assigned to a subset of another 2darray, as long as their shapes match. Recall the 2darray heights_and_ages_arr: heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights _ and heights _ and heights _ and print(heights ages = heights + ages ages_arr = np.array(heights_and_ages) ages_arr = heights_and_ages_arr.reshape((2,45)) and_ages_arr)
  15. If we want to update both height and age of the first president with new data, we can supply the data in a list: heights_and_ages_arr[:, 0] = [190, 58] heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) heights_and_ages_arr[:, 0] = [190, 58] print(heights_and_ages_arr) We can also update data in a subarray with a numpy array as such: new record = 183, 190], [54, 50, 69]]) 42:] = new_record heights_and_ages_arr Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_and_ages = heights + ages heights_and_ages_arr = np.array(heights_and_ages) heights_and_ages_arr = heights_and_ages_arr.reshape((2,45)) new record = np.array([[180, 183, 190], [54, 50, 69]]) 42:] = new_record print(heights_and_ages_arr) Note the last three columns' values have changed. Updating a multidimensional array with a new record is straightforward in numpy as long as their shapes match.
  16. Combining Two Arrays Oftentime we obtain data stored in different arrays and we need to combine them into one to keep it in one place. For example, instead of having the ages stored in a list, it could be stored in a 2darray: ages_arr.shape ages_arr[:3,] Try it Yourself import numpy as np ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] ages_arr = np.array(ages) print(ages_arr.shape) print(ages_arr[:3,]) If we reshape the heights_arr to (45,1), the same as lages_arrl, we can stack them horizontally (by column) to get a 2darray using lhstackl: heights_arr = heights_arr.reshape((45,1)) height_age_arr = np.hstack((heights_arr, ages_arr)) height_age_arr.shape height_age_arr[:3,] Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_arr = np.array(heights) ages_arr = np.array(ages) heights_arr = heights_arr.reshape((45,1)) ages_arr = ages_arr.reshape((45,1)) height_age_arr = np.hstack((heights_arr, ages_arr)) print(height_age_arr.shape) print(height_age_arr[:3,]) Now height_age_arr has both heights and ages for the presidents, each column corresponds to the height and age of one president. Similarly, if we want to combine the arrays vertically (by row), we can use Ivstackl. heights_arr = heights_arr.reshape((1,45)) ages_arr = ages_arr.reshape((1,45))
  17. height_age_arr = np.vstack((heights_arr, ages_arr)) height_age_arr.shape height_age_arr[:,:3] Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_arr = np.array(heights) ages_arr = np.array(ages) heights_arr = heights_arr.reshape((1,45)) ages_arr = ages_arr.reshape((1,45)) height_age_arr = np.vstack((heights_arr, ages_arr)) print(height_age_arr.shape) print(height_age_arr[:,:3]) To combine more than two arrays horizontally, simply add the additional arrays into the tuple. Concatenate More generally, we can use the function numpy.concatenate. If we want to concatenate, link together, two arrays along rows, then pass 'axis = Il to achieve the same result as using numpy.hstack; and pass 'axis = 01 if you want to combine arrays vertically. In the example from the previous part, we were using hstack to combine two arrays horizontally, instead: height_age_arr = np.concatenate((heights_arr, ages_arr), axis=l) Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_arr = np.array(heights) ages_arr = np.array(ages) heights_arr = heights_arr.reshape((45,1)) ages_arr = ages_arr.reshape((45,1)) # height_age_arr = np.hstack((heights_arr, ages_arr)) height_age_arr = np.concatenate((heights_arr, ages_arr), axis=l) print(height_age_arr.shape)
  18. print(height_age_arr[:3,:]) Also you can get the same result as using vstack: height_age_arr = np.concatenate((heights_arr, ages_arr), axis=0) Try it Yourself import numpy as np heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191] ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70] heights_arr = np.array(heights) ages_arr = np.array(ages) heights_arr = heights_arr.reshape((1,45)) ages_arr = ages_arr.reshape((1,45)) #height_age_arr = np.vstack((heights_arr, ages_arr)) height_age_arr = np.concatenate((heights_arr, ages_arr), axis=O) print(height_age_arr.shape) print(height_age_arr[:,:3]) You can use np.hstack to concatenate arrays ONLY if they have the same number of rows.