Pandas Operations Flashcards

1
Q

A.add(B, fill_value=0)

A

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:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A = pd.DataFrame(rng.randint(0, 20, (2, 2)),

columns=list(‘AB’))

A

Columns as list*

Save some typing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

area.index | population.index

A

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:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

A.stack()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
Python Operator	Pandas Method(s)
\+	add()
-	sub(), subtract()
*	mul(), multiply()
/	truediv(), div(), divide()
//	floordiv()
%	mod()
**	pow()
A

The following table lists Python operators and their equivalent Pandas object methods:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

df = pd.DataFrame(A, columns=list(‘QRST’))

df - df.iloc[0]

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

df.subtract(df[‘R’], axis=0)

A

f you would instead like to operate column-wise, you can use the object methods mentioned earlier, while specifying the axis keyword:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly