formations/algo/algofundoc/notebooks/Pandas.ipynb
2018-08-24 14:09:25 +02:00

230 lines
5.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The **import** keyword is used to import a library"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.123233995736766e-17\n"
]
}
],
"source": [
"import math\n",
"print(math.cos(math.pi / 2))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pandas\n",
"======="
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>col1</th>\n",
" <th>col2</th>\n",
" <th>col3</th>\n",
" <th>col4</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>line1</th>\n",
" <td>-0.882125</td>\n",
" <td>2.176452</td>\n",
" <td>0.163955</td>\n",
" <td>-0.618232</td>\n",
" </tr>\n",
" <tr>\n",
" <th>line2</th>\n",
" <td>-0.721538</td>\n",
" <td>0.035578</td>\n",
" <td>0.180072</td>\n",
" <td>1.015987</td>\n",
" </tr>\n",
" <tr>\n",
" <th>line3</th>\n",
" <td>-1.162355</td>\n",
" <td>0.384632</td>\n",
" <td>-0.674092</td>\n",
" <td>0.162693</td>\n",
" </tr>\n",
" <tr>\n",
" <th>line4</th>\n",
" <td>-1.399455</td>\n",
" <td>-0.698512</td>\n",
" <td>0.039420</td>\n",
" <td>0.898408</td>\n",
" </tr>\n",
" <tr>\n",
" <th>line5</th>\n",
" <td>1.755342</td>\n",
" <td>-0.073242</td>\n",
" <td>-1.502503</td>\n",
" <td>-0.586194</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" col1 col2 col3 col4\n",
"line1 -0.882125 2.176452 0.163955 -0.618232\n",
"line2 -0.721538 0.035578 0.180072 1.015987\n",
"line3 -1.162355 0.384632 -0.674092 0.162693\n",
"line4 -1.399455 -0.698512 0.039420 0.898408\n",
"line5 1.755342 -0.073242 -1.502503 -0.586194"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy\n",
"import pandas\n",
"rows = ['line1', 'line2', 'line3', 'line4', 'line5']\n",
"cols = ['col1', 'col2', 'col3', 'col4']\n",
"from IPython.display import display\n",
"dataframe = pandas.DataFrame(numpy.random.randn(5,4), index=rows, columns=cols)\n",
"display(dataframe)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"reorganise a **dataframe** from datas as a dictionary with tuples as keys\n",
"-----------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"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>Column 1</th>\n",
" <th>Column 2</th>\n",
" <th>Column 3</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Cerise</td>\n",
" <td>Lanister</td>\n",
" <td>14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Paul</td>\n",
" <td>Durand</td>\n",
" <td>13</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Pierre</td>\n",
" <td>Dupont</td>\n",
" <td>16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>john</td>\n",
" <td>Snow</td>\n",
" <td>12</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Column 1 Column 2 Column 3\n",
"0 Cerise Lanister 14\n",
"1 Paul Durand 13\n",
"2 Pierre Dupont 16\n",
"3 john Snow 12"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"dico = {('john', 'Snow') : 12, ('Paul', 'Durand') : 13, (\"Pierre\", \"Dupont\") : 16, (\"Cerise\", \"Lanister\") : 14}\n",
"import pandas\n",
"df = pandas.Series(dico).reset_index()\n",
"df.columns = ['Column 1', 'Column 2', 'Column 3']\n",
"from IPython.display import display\n",
"display(df)"
]
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}