1
0
forked from tdwojak/Python2019
Python2019/labs04/Pandas.ipynb
2019-02-09 08:54:43 +01:00

1995 lines
53 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Podstawy Analizy danych w Pythonie: pandas\n",
"\n",
"## 9 lutego 2019"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Ostatnia cześć kursu Pythona będzie dotyczyć biblioteki **pandas**, która służy do analizy danych. Zacznijmy zatem od importu. Przeważnie bibliotekę skraca się do *pd*:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import sys\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Pandas posiada dwie podstawowe struktury danych: \n",
" * szereg (Series),\n",
" * ramka danych (DataFrame). \n",
" \n",
"Zaczniemy od szeregów. Szereg danych, mówiąc prościej jest to lista danych tego samego typu."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Żeby zobaczyć szeregi w akcji, stwórzmy listę losowych liczb:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 4 5 10 9 13 7 7 4 19 13 13 10 2 3 17 18 3 14 4 19 5 6 9 5\n",
" 13 14]\n"
]
}
],
"source": [
"losowe = np.random.randint(1, 20, 26)\n",
"print(losowe)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"A następnie stwórzmy szereg, korzystając z powyższych liczb:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 4\n",
"1 5\n",
"2 10\n",
"3 9\n",
"4 13\n",
"5 7\n",
"6 7\n",
"7 4\n",
"8 19\n",
"9 13\n",
"10 13\n",
"11 10\n",
"12 2\n",
"13 3\n",
"14 17\n",
"15 18\n",
"16 3\n",
"17 14\n",
"18 4\n",
"19 19\n",
"20 5\n",
"21 6\n",
"22 9\n",
"23 5\n",
"24 13\n",
"25 14\n",
"dtype: int64\n"
]
}
],
"source": [
"dane = pd.Series(losowe)\n",
"print(dane)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Czym różni się szereg od listy? Szereg danych posiada indeks, czyli klucz, dzięki ktoremu możemy zindetyfikować dane. Domyślnie, indeks jest ciągiem liczb zaczynających się od zera. Nie musi tak być, możemy podczas tworzenia przekazać również indeks:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a 1\n",
"b 2\n",
"c 3\n",
"d 4\n",
"e 5\n",
"dtype: int64\n"
]
}
],
"source": [
"dane2 = pd.Series([1,2,3,4,5], index=['a', 'b', 'c', 'd', 'e'])\n",
"print(dane2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Jak można domyśleć się, indeks służy nam do uzyskania dostępu do danego elementu:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"print(dane2['b'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Więcej o dostępnie do danych będzie w dalszej części kursu."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Żeby uzyskać rozmiar danych możemy wykorzystać znaną już funkcję **len** lub wykorzystać polę **shape**."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"26\n",
"(26,)\n"
]
}
],
"source": [
"print(len(dane))\n",
"print(dane.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Przeważnie zbiory danych, na których pracujemy są duże. Stąd, próba ich wyświetlenia może okazać się karkołomna\n",
"lub nawet niemożliwa. Czasem chcemy tylko zobaczyć pogląd. Do tego służą dwie metody: **head** i **tail**, które\n",
" zwrócą nam kilka pierwszych lub ostatnich wierszy z szeregu:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 4\n",
"1 5\n",
"2 10\n",
"3 9\n",
"4 13\n",
"dtype: int64\n"
]
}
],
"source": [
"print(dane.head())"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"21 6\n",
"22 9\n",
"23 5\n",
"24 13\n",
"25 14\n",
"dtype: int64\n"
]
}
],
"source": [
"print(dane.tail())"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Szeregi są dostosowane do analizy danych. Np. udostępniają prosty sposób do uzyskania podstawowych statystyk:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Średnia: 9.461538461538462\n",
"Mediana: 9.0\n"
]
}
],
"source": [
"print(\"Średnia:\", dane.mean())\n",
"print(\"Mediana:\", dane.median())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Jak i inne przydatne funkcje:"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Zbiór wartości: [14 6 16 7 17 13 15 2 12 3 4 1 9 19 5]\n",
"Zliczanie 15 4\n",
"13 3\n",
"4 3\n",
"12 2\n",
"7 2\n",
"2 2\n",
"19 1\n",
"17 1\n",
"16 1\n",
"14 1\n",
"9 1\n",
"6 1\n",
"5 1\n",
"3 1\n",
"1 1\n",
"dtype: int64\n"
]
}
],
"source": [
"print(\"Zbiór wartości:\", dane.unique())\n",
"print(\"Zliczanie\", dane.value_counts())"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Metoda ```value_counts``` zwraca nam szereg danych, który możemy wykorzystać do dalszych badań. Na przyklad, żeby wyświetlić 5 najczęściej występujących wartości, możemy napisać:"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15 4\n",
"13 3\n",
"4 3\n",
"12 2\n",
"7 2\n",
"dtype: int64\n"
]
}
],
"source": [
"print(dane.value_counts().head())"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Żeby uzyskać wszystkie podstawowe statystyki, możmey wywołać metodę ```describe```:"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"count 25.000000\n",
"mean 9.720000\n",
"std 5.556678\n",
"min 1.000000\n",
"25% 4.000000\n",
"50% 12.000000\n",
"75% 15.000000\n",
"max 19.000000\n",
"dtype: float64\n"
]
}
],
"source": [
"print(dane.describe())"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"A żeby wyświetlić je w postaci wykresu:"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.axes._subplots.AxesSubplot at 0x7f28547b2f98>"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAW0AAAEACAYAAAB4ayemAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAEFdJREFUeJzt3V+IbedZx/Hfc3LS0jQmc1B7lMZmtKKBgIyigRI5rCq0\noaIVLyRViaNQvEhpICDW3hy8kV5VCuKNjTWGlqghaaqoSSW+lFQ0KU2maZNWA51oJOcYScyQhDY5\n5vFi78mZTGb22n/etZ/3Xe/3A8PsNbNnr9+8Z81v1jz7zzF3FwCgDieiAwAA5kdpA0BFKG0AqAil\nDQAVobQBoCKUNgBU5OQ8VzKzXUkvSHpN0qvuft2QoQAAR5urtDUp687dnx8yDABgtnnHI7bAdQEA\nA5m3iF3SfWb2sJl9eMhAAIDjzTseud7dnzGz75f0RTN7wt0fHDIYAODN5iptd39m+v5ZM7tH0nWS\n3lDaZsaLmADAgtzdFrl+73jEzC4zs8unl98u6X2Svn7MznnL8Hb27NnwDGN6i1rP6U9FwNvQ+z17\n7H6j/61re1vGPGfapyXdMz2TPinps+5+/1J7w1x2d3ejI4wK65nbbnSApvWWtrt/W9LWGrIAAHrw\nML4CbW9vR0cYFdYzt+3oAE2zZecqb7ohM891W8AYmJkuzpjXuuew/dIBizEzee47IrF+KaXoCKPC\neuaWogM0jdIGgIowHgEGwngEfRiPAMDIUdoFYgabF+uZW4oO0DRKGwAqwkwbGAgzb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hCx+dKpc0TbwbxmqTO3X/S3a+LDlOhz2hyPB70MUn/6O4/LukBSb+/9lR1\nOmotJemT7v5T07d/WHeoil2QdKu7XyvpPZJunvblQsfnqmfaPPEmPxOvCbM0d39Q0vOHPvxBXXy4\nw+2SfnmtoSp1zFpKsQ8RqZa7n3P3R6eXX5T0hCaPxlvo+Fy1HHjiTX4u6T4ze9jMPhwdZiTe4e7n\npckPjqR3BOep3c1m9qiZfZpR03LMbFPSlqR/kXR6keOTM7ryXO/uPy3pA5r8cPxsdKAR4t735f2J\npHe7+5akc5I+GZynOmZ2uaS7JN0yPeM+fDzOPD5XLe25n3iD+bj7M9P3z0q6R5MRFFZz3sxOS5KZ\n/YCk/w7OUy13f9YvPuTsTyX9TGSe2pjZSU0K+w53v3f64YWOz1VL+2FJP2pmV5vZWyTdKOkLK95m\ns8zssulvYZnZ2yW9T9LXY1NVyfTGuesXJG1PL/+mpHsPfwGO9Ya1nJbKvl8Rx+ei/kzS4+7+qQMf\nW+j4XPlx2tOH/HxKF59484mVbrBhZvbDmpxduyZPfPos67kYM/ucJv+1yvdKOi/prKTPS/prST8k\n6SlJv+ruvFRdj2PW8r2azGJf0+RFSH5nfx6L2czseklfkvSYJj/jLunjmjzL/K805/HJk2sAoCLc\nEQkAFaG0AaAilDYAVITSBoCKUNoAUBFKGwAqQmkDQEUobQCoyP8DqnDI/zjyvZIAAAAASUVORK5C\nYII=\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x7f285474be10>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"dane.hist()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"(Dane zostały wygenerowane w sposób losowy, stąd ich analiza jak na razie jest pozbawiona sensu.)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Indeksowanie, czyli dostęp do danych"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Stwórzmy szereg danych, którego indeks będzie składać się z wielkich liter alfabetu:"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A 11\n",
"B 13\n",
"C 6\n",
"D 9\n",
"E 18\n",
"dtype: int64\n"
]
}
],
"source": [
"import string\n",
"litery = list(string.ascii_uppercase)\n",
"dane3 = pd.Series(losowe, index=litery)\n",
"print(dane3.head())"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Najprostszym sposobem dostępu do danych jest przez indeks:"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"18\n"
]
}
],
"source": [
"print(dane3['E'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Szeregi udostępniają wiele więcej. Jeżeli chcemy zobaczyć przykłady o kluczach *P*, *Y*, *T*, to możemy podać listę indeksów jako argument:"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"P 12\n",
"Y 9\n",
"T 7\n",
"dtype: int64\n"
]
}
],
"source": [
"print(dane3[['P', 'Y', 'T']])"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Możemy również podać zakres danych:"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"B 13\n",
"C 6\n",
"D 9\n",
"E 18\n",
"dtype: int64\n"
]
}
],
"source": [
"print(dane3['B':'E'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Jeżeli zmienimy indeks szeregu, to cay czas mamy możliwość pracy na indeskach liczbowych:"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"C 6\n",
"D 9\n",
"E 18\n",
"dtype: int64\n"
]
}
],
"source": [
"print(dane3[2:5])"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Mapowanie"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Szeregi pozwalają zmieniać dane, które przechowują. Pojedyńcze wartości mozemy zmieniać przy pomocy odwołania się do konkretnego elementu:"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"777\n"
]
}
],
"source": [
"dane3[2] = 777\n",
"print(dane3[2])"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Jeżeli chcemy zmienić cały szereg przy pomocy funkcji, możemy wykorzystać metodę ```map```:"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A 1331\n",
"B 2197\n",
"C 469097433\n",
"D 729\n",
"E 5832\n",
"F 1000\n",
"G 343\n",
"H 512\n",
"I 125\n",
"J 8\n",
"K 3375\n",
"L 4096\n",
"M 216\n",
"N 5832\n",
"O 343\n",
"P 1728\n",
"Q 4913\n",
"R 1728\n",
"S 2197\n",
"T 343\n",
"U 4913\n",
"V 3375\n",
"W 2744\n",
"X 1331\n",
"Y 729\n",
"Z 4096\n",
"dtype: int64\n"
]
}
],
"source": [
"def cube(x):\n",
" return x ** 3\n",
"print(dane3.map(cube))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"*Uwaga:* w Pythonie istnieją funkcje lambda, które można tu wykorzystać."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Ramki danych"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ramka danych jest odpowiednikiem tabeli znanej z R lub sqla. Patrząc z innego punktu widzenia, jest lista szeregóœ danych, które są połącząne z sobą wspólnym indeksem. Stwórzmy ramkę danych składających się z małych i wielkich liter:"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D'), ('e', 'E'), ('f', 'F'), ('g', 'G'), ('h', 'H'), ('i', 'I'), ('j', 'J'), ('k', 'K'), ('l', 'L'), ('m', 'M'), ('n', 'N'), ('o', 'O'), ('p', 'P'), ('q', 'Q'), ('r', 'R'), ('s', 'S'), ('t', 'T'), ('u', 'U'), ('v', 'V'), ('w', 'W'), ('x', 'X'), ('y', 'Y'), ('z', 'Z')]\n",
" 0 1\n",
"0 a A\n",
"1 b B\n",
"2 c C\n",
"3 d D\n",
"4 e E\n",
"5 f F\n",
"6 g G\n",
"7 h H\n",
"8 i I\n",
"9 j J\n",
"10 k K\n",
"11 l L\n",
"12 m M\n",
"13 n N\n",
"14 o O\n",
"15 p P\n",
"16 q Q\n",
"17 r R\n",
"18 s S\n",
"19 t T\n",
"20 u U\n",
"21 v V\n",
"22 w W\n",
"23 x X\n",
"24 y Y\n",
"25 z Z\n"
]
}
],
"source": [
"wielkie = list(string.ascii_uppercase)\n",
"male = list(string.ascii_lowercase)\n",
"surowe = list(zip(male, wielkie))\n",
"print(surowe)\n",
"\n",
"dane = pd.DataFrame(surowe)\n",
"print(dane)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Jak widzimy, ramkę danych tworzymy podając listę przykładów. W powyższej ramce mamy dwie kolumny nazwane *0* i *1*. Zmieńmy te nazwy na bardziej czytelne:"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" małe wielkie\n",
"0 a A\n",
"1 b B\n",
"2 c C\n",
"3 d D\n",
"4 e E\n"
]
}
],
"source": [
"dane.columns = [\"małe\", \"wielkie\"]\n",
"print(dane.head())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Obsługa ramki danych nie różni się za bardzo od obsługi szeregu, np. działaja metody head i tail, jak i inne:"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"małe z\n",
"wielkie Z\n",
"dtype: object\n",
" małe wielkie\n",
"count 26 26\n",
"unique 26 26\n",
"top j M\n",
"freq 1 1\n"
]
}
],
"source": [
"print(dane.max())\n",
"print(dane.describe())"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Dodajmy trzecią kolumnę składającą się z losowych liczb:"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"dane['losowe'] = np.random.randint(1, 20, 26)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"df = pd.read_csv(\"./titanic_train.tsv\", sep='\\t', index_col='PassengerId')"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Index(['Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch', 'Ticket',\n",
" 'Fare', 'Cabin', 'Embarked'],\n",
" dtype='object')\n"
]
}
],
"source": [
"print(df.columns)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"Int64Index: 891 entries, 1 to 891\n",
"Data columns (total 11 columns):\n",
"Survived 891 non-null int64\n",
"Pclass 891 non-null int64\n",
"Name 891 non-null object\n",
"Sex 891 non-null object\n",
"Age 714 non-null float64\n",
"SibSp 891 non-null int64\n",
"Parch 891 non-null int64\n",
"Ticket 891 non-null object\n",
"Fare 891 non-null float64\n",
"Cabin 204 non-null object\n",
"Embarked 889 non-null object\n",
"dtypes: float64(2), int64(4), object(5)\n",
"memory usage: 83.5+ KB\n"
]
}
],
"source": [
"df.info()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"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>Survived</th>\n",
" <th>Pclass</th>\n",
" <th>Name</th>\n",
" <th>Sex</th>\n",
" <th>Age</th>\n",
" <th>SibSp</th>\n",
" <th>Parch</th>\n",
" <th>Ticket</th>\n",
" <th>Fare</th>\n",
" <th>Cabin</th>\n",
" <th>Embarked</th>\n",
" </tr>\n",
" <tr>\n",
" <th>PassengerId</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",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>Braund\\t Mr. Owen Harris</td>\n",
" <td>male</td>\n",
" <td>22.0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>A/5 21171</td>\n",
" <td>7.2500</td>\n",
" <td>NaN</td>\n",
" <td>S</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>Cumings\\t Mrs. John Bradley (Florence Briggs T...</td>\n",
" <td>female</td>\n",
" <td>38.0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>PC 17599</td>\n",
" <td>71.2833</td>\n",
" <td>C85</td>\n",
" <td>C</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" <td>Heikkinen\\t Miss. Laina</td>\n",
" <td>female</td>\n",
" <td>26.0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>STON/O2. 3101282</td>\n",
" <td>7.9250</td>\n",
" <td>NaN</td>\n",
" <td>S</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>Futrelle\\t Mrs. Jacques Heath (Lily May Peel)</td>\n",
" <td>female</td>\n",
" <td>35.0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>113803</td>\n",
" <td>53.1000</td>\n",
" <td>C123</td>\n",
" <td>S</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>Allen\\t Mr. William Henry</td>\n",
" <td>male</td>\n",
" <td>35.0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>373450</td>\n",
" <td>8.0500</td>\n",
" <td>NaN</td>\n",
" <td>S</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Survived Pclass \\\n",
"PassengerId \n",
"1 0 3 \n",
"2 1 1 \n",
"3 1 3 \n",
"4 1 1 \n",
"5 0 3 \n",
"\n",
" Name Sex Age \\\n",
"PassengerId \n",
"1 Braund\\t Mr. Owen Harris male 22.0 \n",
"2 Cumings\\t Mrs. John Bradley (Florence Briggs T... female 38.0 \n",
"3 Heikkinen\\t Miss. Laina female 26.0 \n",
"4 Futrelle\\t Mrs. Jacques Heath (Lily May Peel) female 35.0 \n",
"5 Allen\\t Mr. William Henry male 35.0 \n",
"\n",
" SibSp Parch Ticket Fare Cabin Embarked \n",
"PassengerId \n",
"1 1 0 A/5 21171 7.2500 NaN S \n",
"2 1 0 PC 17599 71.2833 C85 C \n",
"3 0 0 STON/O2. 3101282 7.9250 NaN S \n",
"4 1 0 113803 53.1000 C123 S \n",
"5 0 0 373450 8.0500 NaN S "
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"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>Survived</th>\n",
" <th>Pclass</th>\n",
" </tr>\n",
" <tr>\n",
" <th>PassengerId</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Survived Pclass\n",
"PassengerId \n",
"1 0 3\n",
"2 1 1\n",
"3 1 3\n",
"4 1 1\n",
"5 0 3"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[['Survived', 'Pclass']].head()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(891, 11)\n"
]
}
],
"source": [
"print(df.shape)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Survived 1\n",
"Pclass 1\n",
"Name Graham\\t Miss. Margaret Edith\n",
"Sex female\n",
"Age 19\n",
"SibSp 0\n",
"Parch 0\n",
"Ticket 112053\n",
"Fare 30\n",
"Cabin B42\n",
"Embarked S\n",
"Name: 888, dtype: object"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[888]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"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>Survived</th>\n",
" <th>Pclass</th>\n",
" <th>Name</th>\n",
" <th>Sex</th>\n",
" <th>Age</th>\n",
" <th>SibSp</th>\n",
" <th>Parch</th>\n",
" <th>Ticket</th>\n",
" <th>Fare</th>\n",
" <th>Cabin</th>\n",
" <th>Embarked</th>\n",
" </tr>\n",
" <tr>\n",
" <th>PassengerId</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",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>888</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>Graham\\t Miss. Margaret Edith</td>\n",
" <td>female</td>\n",
" <td>19.0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>112053</td>\n",
" <td>30.00</td>\n",
" <td>B42</td>\n",
" <td>S</td>\n",
" </tr>\n",
" <tr>\n",
" <th>889</th>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>Johnston\\t Miss. Catherine Helen \"Carrie\"</td>\n",
" <td>female</td>\n",
" <td>NaN</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>W./C. 6607</td>\n",
" <td>23.45</td>\n",
" <td>NaN</td>\n",
" <td>S</td>\n",
" </tr>\n",
" <tr>\n",
" <th>890</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>Behr\\t Mr. Karl Howell</td>\n",
" <td>male</td>\n",
" <td>26.0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>111369</td>\n",
" <td>30.00</td>\n",
" <td>C148</td>\n",
" <td>C</td>\n",
" </tr>\n",
" <tr>\n",
" <th>891</th>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>Dooley\\t Mr. Patrick</td>\n",
" <td>male</td>\n",
" <td>32.0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>370376</td>\n",
" <td>7.75</td>\n",
" <td>NaN</td>\n",
" <td>Q</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Survived Pclass Name \\\n",
"PassengerId \n",
"888 1 1 Graham\\t Miss. Margaret Edith \n",
"889 0 3 Johnston\\t Miss. Catherine Helen \"Carrie\" \n",
"890 1 1 Behr\\t Mr. Karl Howell \n",
"891 0 3 Dooley\\t Mr. Patrick \n",
"\n",
" Sex Age SibSp Parch Ticket Fare Cabin Embarked \n",
"PassengerId \n",
"888 female 19.0 0 0 112053 30.00 B42 S \n",
"889 female NaN 1 2 W./C. 6607 23.45 NaN S \n",
"890 male 26.0 0 0 111369 30.00 C148 C \n",
"891 male 32.0 0 0 370376 7.75 NaN Q "
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[888:892]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"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>Survived</th>\n",
" <th>Pclass</th>\n",
" <th>Name</th>\n",
" <th>Sex</th>\n",
" <th>Age</th>\n",
" <th>SibSp</th>\n",
" <th>Parch</th>\n",
" <th>Ticket</th>\n",
" <th>Fare</th>\n",
" <th>Cabin</th>\n",
" <th>Embarked</th>\n",
" </tr>\n",
" <tr>\n",
" <th>PassengerId</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",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>Cumings\\t Mrs. John Bradley (Florence Briggs T...</td>\n",
" <td>female</td>\n",
" <td>38.0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>PC 17599</td>\n",
" <td>71.2833</td>\n",
" <td>C85</td>\n",
" <td>C</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" <td>Heikkinen\\t Miss. Laina</td>\n",
" <td>female</td>\n",
" <td>26.0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>STON/O2. 3101282</td>\n",
" <td>7.9250</td>\n",
" <td>NaN</td>\n",
" <td>S</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>Futrelle\\t Mrs. Jacques Heath (Lily May Peel)</td>\n",
" <td>female</td>\n",
" <td>35.0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>113803</td>\n",
" <td>53.1000</td>\n",
" <td>C123</td>\n",
" <td>S</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" <td>Johnson\\t Mrs. Oscar W (Elisabeth Vilhelmina B...</td>\n",
" <td>female</td>\n",
" <td>27.0</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>347742</td>\n",
" <td>11.1333</td>\n",
" <td>NaN</td>\n",
" <td>S</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>Nasser\\t Mrs. Nicholas (Adele Achem)</td>\n",
" <td>female</td>\n",
" <td>14.0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>237736</td>\n",
" <td>30.0708</td>\n",
" <td>NaN</td>\n",
" <td>C</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Survived Pclass \\\n",
"PassengerId \n",
"2 1 1 \n",
"3 1 3 \n",
"4 1 1 \n",
"9 1 3 \n",
"10 1 2 \n",
"\n",
" Name Sex Age \\\n",
"PassengerId \n",
"2 Cumings\\t Mrs. John Bradley (Florence Briggs T... female 38.0 \n",
"3 Heikkinen\\t Miss. Laina female 26.0 \n",
"4 Futrelle\\t Mrs. Jacques Heath (Lily May Peel) female 35.0 \n",
"9 Johnson\\t Mrs. Oscar W (Elisabeth Vilhelmina B... female 27.0 \n",
"10 Nasser\\t Mrs. Nicholas (Adele Achem) female 14.0 \n",
"\n",
" SibSp Parch Ticket Fare Cabin Embarked \n",
"PassengerId \n",
"2 1 0 PC 17599 71.2833 C85 C \n",
"3 0 0 STON/O2. 3101282 7.9250 NaN S \n",
"4 1 0 113803 53.1000 C123 S \n",
"9 0 2 347742 11.1333 NaN S \n",
"10 1 0 237736 30.0708 NaN C "
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[df.Survived == 1].head()"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"PassengerId\n",
"1 2\n",
"2 2\n",
"3 1\n",
"4 2\n",
"5 1\n",
"Name: FSize, dtype: int64"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[\"FSize\"] = df.SibSp + df.Parch + 1\n",
"df.FSize.head()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"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>Survived</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Sex</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>female</th>\n",
" <td>0.742038</td>\n",
" </tr>\n",
" <tr>\n",
" <th>male</th>\n",
" <td>0.188908</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Survived\n",
"Sex \n",
"female 0.742038\n",
"male 0.188908"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[['Sex', 'Survived']].groupby('Sex').mean()"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"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>Survived</th>\n",
" </tr>\n",
" <tr>\n",
" <th>FSize</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.303538</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Survived\n",
"FSize \n",
"1 0.303538"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[['FSize', 'Survived']].loc[df['FSize'] == 1].groupby('FSize').mean()"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0.30353817504655495"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[['FSize', 'Survived']].groupby('FSize').mean().loc[1]"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Survived 0\n",
"Pclass 3\n",
"Name Braund\\t Mr. Owen Harris\n",
"Sex male\n",
"Age 22\n",
"SibSp 1\n",
"Parch 0\n",
"Ticket A/5 21171\n",
"Fare 7.25\n",
"Cabin NaN\n",
"Embarked S\n",
"FSize 2\n",
"Name: 1, dtype: object\n",
"Survived 1\n",
"Pclass 1\n",
"Name Cumings\\t Mrs. John Bradley (Florence Briggs T...\n",
"Sex female\n",
"Age 38\n",
"SibSp 1\n",
"Parch 0\n",
"Ticket PC 17599\n",
"Fare 71.2833\n",
"Cabin C85\n",
"Embarked C\n",
"FSize 2\n",
"Name: 2, dtype: object\n",
"Survived 1\n",
"Pclass 3\n",
"Name Heikkinen\\t Miss. Laina\n",
"Sex female\n",
"Age 26\n",
"SibSp 0\n",
"Parch 0\n",
"Ticket STON/O2. 3101282\n",
"Fare 7.925\n",
"Cabin NaN\n",
"Embarked S\n",
"FSize 1\n",
"Name: 3, dtype: object\n",
"Survived 1\n",
"Pclass 1\n",
"Name Futrelle\\t Mrs. Jacques Heath (Lily May Peel)\n",
"Sex female\n",
"Age 35\n",
"SibSp 1\n",
"Parch 0\n",
"Ticket 113803\n",
"Fare 53.1\n",
"Cabin C123\n",
"Embarked S\n",
"FSize 2\n",
"Name: 4, dtype: object\n"
]
}
],
"source": [
"for idx, row in df.iterrows():\n",
" if idx < 5:\n",
" print(row)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Slideshow",
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}