**Examples**

In [1]:

```
import numpy as np
import pandas as pd
```

In [2]:

```
df = pd.DataFrame({"P": ["f1", "f1", "f1", "f1", "f1",
"b1", "b1", "b1", "b1"],
"Q": ["one", "one", "one", "two", "two",
"one", "one", "two", "two"],
"R": ["small", "large", "large", "small",
"small", "large", "small", "small",
"large"],
"S": [1, 2, 2, 3, 3, 4, 5, 6, 7],
"T": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
df
```

Out[2]:

This first example aggregates values by taking the sum:

In [3]:

```
table = pd.pivot_table(df, values='S', index=['P', 'Q'],
columns=['R'], aggfunc=np.sum)
table
```

Out[3]:

You can also fill missing values using the fill_value parameter:

In [4]:

```
table = pd.pivot_table(df, values='S', index=['P', 'Q'],
columns=['R'], aggfunc=np.sum, fill_value=0)
table
```

Out[4]:

Following example aggregates by taking the mean across multiple columns.

In [5]:

```
table = pd.pivot_table(df, values=['S', 'T'], index=['P', 'R'],
aggfunc={'S': np.mean,
'T': np.mean})
table
```

Out[5]:

You can also calculate multiple types of aggregations for any

given value column:

table = pd.pivot_table(df, values=['S', 'T'], index=['P', 'R'], aggfunc={'S': np.mean, 'T': [min, max, np.mean]}) table