Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython
Rate it:
11%
Flag icon
The main restriction on function arguments is that the keyword arguments must follow the positional arguments (if any). You can specify keyword arguments in any order;
11%
Flag icon
I generally discourage use of the global keyword. Typically global variables are used to store some kind of state in a system. If you find yourself using a lot of them, it may indicate a need for object-oriented programming
11%
Flag icon
Python has support for so-called anonymous or lambda functions, which are a way of writing functions consisting of a single statement, the result of which is the return value. They are defined with the lambda keyword, which has no meaning other than “we are declaring an anonymous function”:
12%
Flag icon
One reason lambda functions are called anonymous functions is that , unlike functions declared with the def keyword, the function object itself is never given an explicit __name__ attribute.
12%
Flag icon
An iterator is any object that will yield objects to the Python interpreter when used in a context like a for loop.
12%
Flag icon
Most methods expecting a list or list-like object will also accept any iterable object.
12%
Flag icon
A generator is a concise way to construct a new iterable object. Whereas normal functions execute and return a single result at a time, generators return a sequence of multiple results lazily, pausing after each one until the next one is requested. To create a generator, use the yield keyword instead of return in a function:
12%
Flag icon
When you actually call the generator, no code is immediately executed:
12%
Flag icon
It is not until you request elements from the generator that it begins executing its code:
12%
Flag icon
itertools
Jake Losh
itertools is underrated
14%
Flag icon
An ndarray is a generic multidimensional container for homogeneous data; that is, all of the elements must be the same type.
14%
Flag icon
empty creates an array without initializing its values to any particular value.
14%
Flag icon
It’s not safe to assume that np.empty will return an array of all zeros. In some cases, it may return uninitialized “garbage” values.
14%
Flag icon
arange is an array-valued version of the built-in Python range function:
15%
Flag icon
It’s important to be cautious when using the numpy.string_ type, as string data in NumPy is fixed size and may truncate input without warning. pandas has more intuitive out-of-the-box behavior on non-numeric data.
15%
Flag icon
Calling astype always creates a new array (a copy of the data), even if the new dtype is the same as the old dtype.
15%
Flag icon
Arithmetic operations with scalars propagate the scalar argument to each element in the array:
15%
Flag icon
Comparisons between arrays of the same size yield boolean arrays:
15%
Flag icon
Evaluating operations between differently sized arrays is called broadcasting and will be discussed in more detail in Appendix A
15%
Flag icon
An important first distinction from Python’s built-in lists is that array slices are views on the original array. This means that the data is not copied, and any modifications to the view will be reflected in the source array.
15%
Flag icon
As NumPy has been designed to be able to work with very large arrays, you could imagine performance and memory problems if NumPy insisted on always copying data.
15%
Flag icon
If you want a copy of a slice of an ndarray instead of a view, you will need to explicitly copy the array — for example, arr[5:8].copy().
15%
Flag icon
you can pass a comma-separated list of indices to select individual elements.
16%
Flag icon
Selecting data from an array by boolean indexing always creates a copy of the data, even if the returned array is unchanged.
16%
Flag icon
The Python keywords and and or do not work with boolean arrays. Use & (and) and | (or) instead.
17%
Flag icon
Passing multiple index arrays does something slightly different; it selects a one-dimensional array of elements corresponding to each tuple of indices:
17%
Flag icon
Keep in mind that fancy indexing, unlike slicing, always copies the data into a new array.
17%
Flag icon
swapaxes similarly returns a view on the data without making a copy.
17%
Flag icon
A universal function, or ufunc, is a function that performs element-wise operations on data in ndarrays.
17%
Flag icon
While not common, a ufunc can return multiple arrays. modf is one example, a vectorized version of the built-in Python divmod; it returns the fractional and integral parts of a floating-point array:
18%
Flag icon
Ufuncs accept an optional out argument that allows them to operate in-place on arrays:
18%
Flag icon
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.
20%
Flag icon
Python’s built-in random module, by contrast, only samples one value at a time. As you can see from this benchmark, numpy.random is well over an order of magnitude faster for generating very large samples:
21%
Flag icon
Note that using argmax here is not always efficient because it always makes a full scan of the array.
21%
Flag icon
While pandas adopts many coding idioms from NumPy, the biggest difference is that pandas is designed for working with tabular or heterogeneous data. NumPy, by contrast, is best suited for working with homogeneous numerical array data.
21%
Flag icon
Using NumPy functions or NumPy-like operations, such as filtering with a boolean array, scalar multiplication, or applying math functions, will preserve the index-value link:
21%
Flag icon
Another way to think about a Series is as a fixed-length, ordered dict, as it is a mapping of index values to data values. It can be used in many contexts where you might use a dict:
21%
Flag icon
When you are only passing a dict, the index in the resulting Series will have the dict’s keys in sorted order. You can override this by passing the dict keys in the order you want them to appear in the resulting Series:
Jake Losh
I did not find this to be true.
21%
Flag icon
Both the Series object itself and its index have a name attribute, which integrates with other key areas of pandas functionality:
22%
Flag icon
There are many ways to construct a DataFrame, though one of the most common is from a dict of equal-length lists or NumPy arrays:
22%
Flag icon
If you specify a sequence of columns, the DataFrame’s columns will be arranged in that order:
22%
Flag icon
If you pass a column that isn’t contained in the dict, it will appear with missing values in the result:
22%
Flag icon
Attribute-like access (e.g., frame2.year) and tab completion of column names in IPython is provided as a convenience. frame2[column] works for any column name, but frame2.column only works when the column name is a valid Python variable name.
22%
Flag icon
New columns cannot be created with the frame2.eastern syntax.
22%
Flag icon
The column returned from indexing a DataFrame is a view on the underlying data, not a copy. Thus, any in-place modifications to the Series will be reflected in the DataFrame. The column can be explicitly copied with the Series’s copy method.
22%
Flag icon
You can transpose the DataFrame (swap rows and columns) with similar syntax to a NumPy array:
23%
Flag icon
Index objects are immutable and thus can’t be modified by the user:
23%
Flag icon
Immutability makes it safer to share Index objects among data structures:
23%
Flag icon
In addition to being array-like, an Index also behaves like a fixed-size set:
23%
Flag icon
Unlike Python sets, a pandas Index can contain duplicate labels:
« Prev 1 3