python-pour-finance/03-Pandas/05-Groupby.ipynb

390 lines
17 KiB
Plaintext
Raw Permalink Normal View History

2023-08-21 15:12:19 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Groupby\n",
"\n",
"La méthode de regroupement par groupe vous permet de regrouper des lignes de données et d'appeler des fonctions d'agrégation."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd\n",
"# Création dataframe\n",
"data = {'Company':['GOOG','GOOG','MSFT','MSFT','FB','FB'],\n",
" 'Person':['Sam','Charlie','Amy','Vanessa','Carl','Sarah'],\n",
" 'Sales':[200,120,340,124,243,350]}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(data)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Company Person Sales\n",
"0 GOOG Sam 200\n",
"1 GOOG Charlie 120\n",
"2 MSFT Amy 340\n",
"3 MSFT Vanessa 124\n",
"4 FB Carl 243\n",
"5 FB Sarah 350"
],
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Company</th>\n <th>Person</th>\n <th>Sales</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>GOOG</td>\n <td>Sam</td>\n <td>200</td>\n </tr>\n <tr>\n <th>1</th>\n <td>GOOG</td>\n <td>Charlie</td>\n <td>120</td>\n </tr>\n <tr>\n <th>2</th>\n <td>MSFT</td>\n <td>Amy</td>\n <td>340</td>\n </tr>\n <tr>\n <th>3</th>\n <td>MSFT</td>\n <td>Vanessa</td>\n <td>124</td>\n </tr>\n <tr>\n <th>4</th>\n <td>FB</td>\n <td>Carl</td>\n <td>243</td>\n </tr>\n <tr>\n <th>5</th>\n <td>FB</td>\n <td>Sarah</td>\n <td>350</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {},
"execution_count": 3
}
],
"source": [
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Maintenant vous pouvez utiliser la méthode .groupby() pour regrouper les lignes en fonction d'un nom de colonne. Par exemple, groupons les personnes par société. Ceci créera un objet DataFrameGroupBy :**"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<pandas.core.groupby.generic.DataFrameGroupBy object at 0x00000166211DA848>"
]
},
"metadata": {},
"execution_count": 4
}
],
"source": [
"df.groupby('Company')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Vous pouvez sauvegarder cet objet comme nouvelle variable :"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"by_comp = df.groupby(\"Company\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Et ensuite, appelez les méthodes d'agrégation sur l'objet :"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Sales\n",
"Company \n",
"FB 296.5\n",
"GOOG 160.0\n",
"MSFT 232.0"
],
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Sales</th>\n </tr>\n <tr>\n <th>Company</th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>FB</th>\n <td>296.5</td>\n </tr>\n <tr>\n <th>GOOG</th>\n <td>160.0</td>\n </tr>\n <tr>\n <th>MSFT</th>\n <td>232.0</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {},
"execution_count": 6
}
],
"source": [
"by_comp.mean()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Sales\n",
"Company \n",
"FB 296.5\n",
"GOOG 160.0\n",
"MSFT 232.0"
],
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Sales</th>\n </tr>\n <tr>\n <th>Company</th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>FB</th>\n <td>296.5</td>\n </tr>\n <tr>\n <th>GOOG</th>\n <td>160.0</td>\n </tr>\n <tr>\n <th>MSFT</th>\n <td>232.0</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {},
"execution_count": 7
}
],
"source": [
"df.groupby('Company').mean()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Autres exemples de méthodes d'agrégation :"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Sales\n",
"Company \n",
"FB 75.660426\n",
"GOOG 56.568542\n",
"MSFT 152.735065"
],
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Sales</th>\n </tr>\n <tr>\n <th>Company</th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>FB</th>\n <td>75.660426</td>\n </tr>\n <tr>\n <th>GOOG</th>\n <td>56.568542</td>\n </tr>\n <tr>\n <th>MSFT</th>\n <td>152.735065</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {},
"execution_count": 8
}
],
"source": [
"by_comp.std()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Person Sales\n",
"Company \n",
"FB Carl 243\n",
"GOOG Charlie 120\n",
"MSFT Amy 124"
],
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Person</th>\n <th>Sales</th>\n </tr>\n <tr>\n <th>Company</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>FB</th>\n <td>Carl</td>\n <td>243</td>\n </tr>\n <tr>\n <th>GOOG</th>\n <td>Charlie</td>\n <td>120</td>\n </tr>\n <tr>\n <th>MSFT</th>\n <td>Amy</td>\n <td>124</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {},
"execution_count": 9
}
],
"source": [
"by_comp.min()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Person Sales\n",
"Company \n",
"FB Sarah 350\n",
"GOOG Sam 200\n",
"MSFT Vanessa 340"
],
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Person</th>\n <th>Sales</th>\n </tr>\n <tr>\n <th>Company</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>FB</th>\n <td>Sarah</td>\n <td>350</td>\n </tr>\n <tr>\n <th>GOOG</th>\n <td>Sam</td>\n <td>200</td>\n </tr>\n <tr>\n <th>MSFT</th>\n <td>Vanessa</td>\n <td>340</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {},
"execution_count": 10
}
],
"source": [
"by_comp.max()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Person Sales\n",
"Company \n",
"FB 2 2\n",
"GOOG 2 2\n",
"MSFT 2 2"
],
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Person</th>\n <th>Sales</th>\n </tr>\n <tr>\n <th>Company</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>FB</th>\n <td>2</td>\n <td>2</td>\n </tr>\n <tr>\n <th>GOOG</th>\n <td>2</td>\n <td>2</td>\n </tr>\n <tr>\n <th>MSFT</th>\n <td>2</td>\n <td>2</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {},
"execution_count": 11
}
],
"source": [
"by_comp.count()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Sales \n",
" count mean std min 25% 50% 75% max\n",
"Company \n",
"FB 2.0 296.5 75.660426 243.0 269.75 296.5 323.25 350.0\n",
"GOOG 2.0 160.0 56.568542 120.0 140.00 160.0 180.00 200.0\n",
"MSFT 2.0 232.0 152.735065 124.0 178.00 232.0 286.00 340.0"
],
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead tr th {\n text-align: left;\n }\n\n .dataframe thead tr:last-of-type th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr>\n <th></th>\n <th colspan=\"8\" halign=\"left\">Sales</th>\n </tr>\n <tr>\n <th></th>\n <th>count</th>\n <th>mean</th>\n <th>std</th>\n <th>min</th>\n <th>25%</th>\n <th>50%</th>\n <th>75%</th>\n <th>max</th>\n </tr>\n <tr>\n <th>Company</th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>FB</th>\n <td>2.0</td>\n <td>296.5</td>\n <td>75.660426</td>\n <td>243.0</td>\n <td>269.75</td>\n <td>296.5</td>\n <td>323.25</td>\n <td>350.0</td>\n </tr>\n <tr>\n <th>GOOG</th>\n <td>2.0</td>\n <td>160.0</td>\n <td>56.568542</td>\n <td>120.0</td>\n <td>140.00</td>\n <td>160.0</td>\n <td>180.00</td>\n <td>200.0</td>\n </tr>\n <tr>\n <th>MSFT</th>\n <td>2.0</td>\n <td>232.0</td>\n <td>152.735065</td>\n <td>124.0</td>\n <td>178.00</td>\n <td>232.0</td>\n <td>286.00</td>\n <td>340.0</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {},
"execution_count": 12
}
],
"source": [
"by_comp.describe()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Company FB GOOG MSFT\n",
"Sales count 2.000000 2.000000 2.000000\n",
" mean 296.500000 160.000000 232.000000\n",
" std 75.660426 56.568542 152.735065\n",
" min 243.000000 120.000000 124.000000\n",
" 25% 269.750000 140.000000 178.000000\n",
" 50% 296.500000 160.000000 232.000000\n",
" 75% 323.250000 180.000000 286.000000\n",
" max 350.000000 200.000000 340.000000"
],
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Company</th>\n <th>FB</th>\n <th>GOOG</th>\n <th>MSFT</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th rowspan=\"8\" valign=\"top\">Sales</th>\n <th>count</th>\n <td>2.000000</td>\n <td>2.000000</td>\n <td>2.000000</td>\n </tr>\n <tr>\n <th>mean</th>\n <td>296.500000</td>\n <td>160.000000</td>\n <td>232.000000</td>\n </tr>\n <tr>\n <th>std</th>\n <td>75.660426</td>\n <td>56.568542</td>\n <td>152.735065</td>\n </tr>\n <tr>\n <th>min</th>\n <td>243.000000</td>\n <td>120.000000</td>\n <td>124.000000</td>\n </tr>\n <tr>\n <th>25%</th>\n <td>269.750000</td>\n <td>140.000000</td>\n <td>178.000000</td>\n </tr>\n <tr>\n <th>50%</th>\n <td>296.500000</td>\n <td>160.000000</td>\n <td>232.000000</td>\n </tr>\n <tr>\n <th>75%</th>\n <td>323.250000</td>\n <td>180.000000</td>\n <td>286.000000</td>\n </tr>\n <tr>\n <th>max</th>\n <td>350.000000</td>\n <td>200.000000</td>\n <td>340.000000</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {},
"execution_count": 13
}
],
"source": [
"by_comp.describe().transpose()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Sales count 2.000000\n",
" mean 160.000000\n",
" std 56.568542\n",
" min 120.000000\n",
" 25% 140.000000\n",
" 50% 160.000000\n",
" 75% 180.000000\n",
" max 200.000000\n",
"Name: GOOG, dtype: float64"
]
},
"metadata": {},
"execution_count": 14
}
],
"source": [
"by_comp.describe().transpose()['GOOG']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bon travail!"
]
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3.7.9 64-bit ('pyfinance': conda)",
"metadata": {
"interpreter": {
"hash": "e89404a230d8800c54ad520c7b67d1bd9bb833a07b37dd3e521a178a3dc34904"
}
}
},
"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.9-final"
}
},
"nbformat": 4,
"nbformat_minor": 1
}