{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Examples**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame([[2, 3, 4],\n",
" [5, 6, 7],\n",
" [8, 9, 10],\n",
" [np.nan, np.nan, np.nan]],\n",
" columns=['P', 'Q', 'R'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Aggregate 'sum' and 'min' functions over the rows:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" P | \n",
" Q | \n",
" R | \n",
"
\n",
" \n",
" \n",
" \n",
" sum | \n",
" 15.0 | \n",
" 18.0 | \n",
" 21.0 | \n",
"
\n",
" \n",
" min | \n",
" 2.0 | \n",
" 3.0 | \n",
" 4.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" P Q R\n",
"sum 15.0 18.0 21.0\n",
"min 2.0 3.0 4.0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.agg(['sum', 'min'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Different aggregations per column:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" P | \n",
" Q | \n",
"
\n",
" \n",
" \n",
" \n",
" max | \n",
" NaN | \n",
" 9.0 | \n",
"
\n",
" \n",
" min | \n",
" 2.0 | \n",
" 3.0 | \n",
"
\n",
" \n",
" sum | \n",
" 15.0 | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" P Q\n",
"max NaN 9.0\n",
"min 2.0 3.0\n",
"sum 15.0 NaN"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.agg({'P' : ['sum', 'min'], 'Q' : ['min', 'max']})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Aggregate over the columns:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 3.0\n",
"1 6.0\n",
"2 9.0\n",
"3 NaN\n",
"dtype: float64"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.agg(\"mean\", axis=\"columns\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}