Pivot Table Flashcards

1
Q

pivot table

A

The pivot table takes simple column-wise data as input, and groups the entries into a two-dimensional table that provides a multidimensional summarization of the data

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

titanic.pivot_table(‘survived’, index=’sex’, columns=’class’)

A

Equivalent to:

titanic.groupby([‘sex’, ‘class’])[‘survived’].aggregate(‘mean’).unstack()

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

age = pd.cut(titanic[‘age’], [0, 18, 80])

titanic.pivot_table(‘survived’, [‘sex’, age], ‘class’)

A

pivot tables can be specified with multiple levels.

looking at age as a third dimension.

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

fare = pd.qcut(titanic[‘fare’], 2)

titanic.pivot_table(‘survived’, [‘sex’, age], [fare, ‘class’])

A

same strategy when working with the columns.

add info on the fare paid using pd.qcut to automatically compute quantiles

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

DataFrame.pivot_table(data, values=None, index=None, columns=None,
aggfunc=’mean’, fill_value=None, margins=False,
dropna=True, margins_name=’All’)

A

The full call signature of the pivot_table method

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

The aggfunc keyword controls what type of aggregation is applied

A

As in the GroupBy, the aggregation specification can be a string representing one of several common choices (e.g., ‘sum’, ‘mean’, ‘count’, ‘min’, ‘max’, etc.) or a function that implements an aggregation (e.g., np.sum(), min(), sum(), etc.). Additionally, it can be specified as a dictionary mapping a column to any of the above desired options:

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

Pivot aggfunc ex:

A

titanic.pivot_table(index=’sex’, columns=’class’,

aggfunc={‘survived’:sum, ‘fare’:’mean’})

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

titanic.pivot_table(‘survived’, index=’sex’, columns=’class’, margins=True)

A

Marings: compute totals along each grouping

Total All Values

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

pd.cut(titanic[‘age’], [0, 18, 80])

A
Use `cut` when you need to segment and sort data values into bins. This
function is also useful for going from a continuous variable to a
categorical variable. For example, `cut` could convert ages to groups of
age ranges. Supports binning into an equal number of bins, or a
pre-specified array of bins.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

pd.qcut(titanic[‘fare’], 2)

A

Signature: pd.qcut(x, q, labels=None, retbins=False, precision=3, duplicates=’raise’)
Docstring:
Quantile-based discretization function. Discretize variable into
equal-sized buckets based on rank or based on sample quantiles. For example
1000 values for 10 quantiles would produce a Categorical object indicating
quantile membership for each data point.

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