Adding and removing data
1. Adding and removing data
The next stop on our NumPy tour is adding and deleting array elements, an important skill as data is almost never perfectly suited to a task right off the bat.2. Concatenating in NumPy
In NumPy, concatenation refers to adding data to an array along any existing axis, such as adding columns to a 2D array. We concatenate using the np-dot-concatenate function.3. Concatenating rows
Let's go back to our classroom_ids_and_sizes array and add information for two more classrooms, also stored in an array. To concatenate the two arrays, pass a tuple of the array names to np-dot-concatenate. In this case, we are concatenating along the first axis - that is, adding new rows. Since concatenating along the first axis is np-dot-concatenate's default behavior, we don't need to explicitly state which axis to concatenate along.4. Concatenating columns
To concatenate along other dimensions, set the axis keyword argument. For example, to add grade level and teacher names, concatenate the array containing this information along the second axis, which has an index of one. The result is two new columns of information for each classroom.5. Shape compatibility
The arrays to be concatenated must have compatible shapes. Specifically, they must have the same shape along all axes except the one being concatenated along. For example, concatenating a three by three array with a four by two array results in a value error no matter which axis we concatenate along, because no part of these shapes is compatible. The value error reminds us that the array dimensions for the concatenation axis must match exactly. We can concatenate a three by three array with a three by two array column-wise, because the axis we are concatenating along, the second axis, is the only axis which does not have the same length.6. Dimension compatibility
The two arrays must also have the same number of dimensions. This is especially important to remember when concatenating a single row or column of data, usually held in a 1D array, with a 2D array. In order to append a single row or column, turn the 1D array into a 2D array using the dot-reshape method before appending.7. Creating compatibility
To reshape a 1D array in preparation for concatenation with a 2D array, indicate whether the data is vertical or horizontal by setting a value of one as the length of the flat dimension. An array containing a single column, like the one here, will have a length of one along the second axis. An array containing a single row will have a length of one along the first axis.8. Concatenating new dimensions
It is not possible to add new dimensions with np-dot-concatenate, since the function only adds data along an existing axis. It is possible to add new dimensions using other functions which we'll discuss later in the course.9. Deleting with np.delete()
NumPy's np-dot-delete function takes three arguments: the array to delete from, a slice, index, or array of indices to be deleted, and the axis to be deleted along. For example, to delete the second row from a 2D array, the index to delete will be one, and the deletion will occur along the first axis, represented with a zero.10. Deleting columns
To delete the second column instead, update the axis keyword argument to one. Now, our class size column is gone.11. Deleting without an axis
If no axis is specified, NumPy deletes the indicated index or indices along a flattened version of the array. If your code outputs an unexpected flattened array, you've probably forgotten the axis argument!12. Let's practice!
Practice with axes and shape compatibility topics from this lesson will serve us well when tackling array math in the next chapter. But first, it's time to try out our concatenating and deleting skills!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.