Python for Data Analysis - Data Wrangling Chapter 4
While NumPy provides the computational foundation for these operations, you will likely want to use pandas as your basis for most kinds of data analysis (especially for structured or tabular data) as it provides a rich, high-level interface making most common data tasks very concise and simple.
There are a number of other functions for creating new arrays.
-
zeros
andones
create arrays of 0’s or 1’s, respectively, with a given length or shape. -
empty
creates an array without initializing its values to any particular value.
An important first distinction from lists is that array slices are views on the original array.
In a high-dimensional numpy array, individual elements can be accessed recursively, or by passing a comma-separated list of indices to select individual elements.
In boolean indexing, either != or - can be used to negate the condition
A universal function, or ufunc
, is a function that performs elementwise operations on data in ndarrays. You can think of them as fast vectorized wrappers for simple functions that take one or more scalar values and produce one or more scalar results.
Others, such as add
or maximum
, take 2 arrays (thus, binary ufuncs) and return a single array as the result.
In general, vectorized array operations will often be one or two (or more) orders of magnitude faster than their pure Python equivalents, with the biggest impact in any kind of numerical computations.
np.save
and np.load
are the two workhorse functions for efficiently saving and loading array data on disk.
Arrays are saved by default in an uncompressed raw binary format with file extension .npy.