Pandas Operations Flashcards
A.add(B, fill_value=0)
If using NaN values is not the desired behavior, the fill value can be modified using appropriate object methods in place of the operators. For example, calling A.add(B) is equivalent to calling A + B, but allows optional explicit specification of the fill value for any elements in A or B that might be missing:
A = pd.DataFrame(rng.randint(0, 20, (2, 2)),
columns=list(‘AB’))
Columns as list*
Save some typing
area.index | population.index
On a series
The resulting array contains the union of indices of the two input arrays, which could be determined using standard Python set arithmetic on these indices:
A.stack()
Stack the prescribed level(s) from columns to index.
Return a reshaped DataFrame or Series having a multi-level
index with one or more new inner-most levels compared to the current
DataFrame. The new inner-most levels are created by pivoting the
columns of the current dataframe:
- if the columns have a single level, the output is a Series;
- if the columns have multiple levels, the new index
level(s) is (are) taken from the prescribed level(s) and
the output is a DataFrame.
The new index levels are sorted.
Python Operator Pandas Method(s) \+ add() - sub(), subtract() * mul(), multiply() / truediv(), div(), divide() // floordiv() % mod() ** pow()
The following table lists Python operators and their equivalent Pandas object methods:
df = pd.DataFrame(A, columns=list(‘QRST’))
df - df.iloc[0]
According to NumPy’s broadcasting rules (see Computation on Arrays: Broadcasting), subtraction between a two-dimensional array and one of its rows is applied row-wise.
df.subtract(df[‘R’], axis=0)
f you would instead like to operate column-wise, you can use the object methods mentioned earlier, while specifying the axis keyword: