|
154 | 154 | "execution_count": null, |
155 | 155 | "metadata": {}, |
156 | 156 | "outputs": [], |
157 | | - "source": [ |
158 | | - "help(pd.read_table)" |
159 | | - ] |
| 157 | + "source": [] |
160 | 158 | }, |
161 | 159 | { |
162 | 160 | "cell_type": "markdown", |
|
268 | 266 | "metadata": {}, |
269 | 267 | "outputs": [], |
270 | 268 | "source": [ |
271 | | - "df = pd.read_table(\"data/titanic_no_header.csv\", sep=\",\", header=None) \n", |
| 269 | + "df = pd.read_table(\"data/titanic_no_header.csv\", \n", |
| 270 | + " sep=\",\", header=None) \n", |
272 | 271 | "df.head(n=3) " |
273 | 272 | ] |
274 | 273 | }, |
|
288 | 287 | "outputs": [], |
289 | 288 | "source": [ |
290 | 289 | "df = pd.read_table(\"data/titanic_no_header.csv\", sep=\",\", \n", |
291 | | - " names = [\"name\", \"column2\", \"age\", \"column4\", \"blip\", \"bloop\", \"spam\", \"eggs\"]) \n", |
| 290 | + " names = [\"name\", \"column2\", \n", |
| 291 | + " \"age\", \"column4\", \n", |
| 292 | + " \"blip\", \"bloop\", \"spam\", \"eggs\"]) \n", |
292 | 293 | "# As you can see, we can choose our own names, whether they make sense or not.\n", |
293 | 294 | "\n", |
294 | 295 | "df.head(3) " |
|
402 | 403 | "df.head(3)" |
403 | 404 | ] |
404 | 405 | }, |
| 406 | + { |
| 407 | + "cell_type": "code", |
| 408 | + "execution_count": null, |
| 409 | + "metadata": {}, |
| 410 | + "outputs": [], |
| 411 | + "source": [ |
| 412 | + "df.columns" |
| 413 | + ] |
| 414 | + }, |
405 | 415 | { |
406 | 416 | "cell_type": "markdown", |
407 | 417 | "metadata": {}, |
|
520 | 530 | "* **`df.shape`** returns a tuple with the numbers of rows and columns: `(row_count, col_count)`." |
521 | 531 | ] |
522 | 532 | }, |
| 533 | + { |
| 534 | + "cell_type": "code", |
| 535 | + "execution_count": null, |
| 536 | + "metadata": {}, |
| 537 | + "outputs": [], |
| 538 | + "source": [ |
| 539 | + "df.shape" |
| 540 | + ] |
| 541 | + }, |
523 | 542 | { |
524 | 543 | "cell_type": "code", |
525 | 544 | "execution_count": null, |
|
594 | 613 | "df.head(3)" |
595 | 614 | ] |
596 | 615 | }, |
| 616 | + { |
| 617 | + "cell_type": "code", |
| 618 | + "execution_count": null, |
| 619 | + "metadata": {}, |
| 620 | + "outputs": [], |
| 621 | + "source": [ |
| 622 | + "df.columns" |
| 623 | + ] |
| 624 | + }, |
597 | 625 | { |
598 | 626 | "cell_type": "markdown", |
599 | 627 | "metadata": {}, |
|
722 | 750 | "outputs": [], |
723 | 751 | "source": [ |
724 | 752 | "# for instance, I may want to change the labels a bit here:\n", |
725 | | - "df.Pclass= df.Pclass.cat.rename_categories( {1:'I',2:\"II\",3:\"III\"})\n", |
| 753 | + "df.Pclass= df.Pclass.cat.rename_categories( {1:'I',\n", |
| 754 | + " 2:\"II\",\n", |
| 755 | + " 3:\"III\"})\n", |
726 | 756 | "df.Pclass" |
727 | 757 | ] |
728 | 758 | }, |
|
733 | 763 | "[datetime64](https://pandas.pydata.org/docs/user_guide/timeseries.html) and [category](https://towardsdatascience.com/staying-sane-while-adopting-pandas-categorical-datatypes-78dbd19dcd8a) are fairly specific, and we refer you to the provided links if you want to learn more about them.\n" |
734 | 764 | ] |
735 | 765 | }, |
736 | | - { |
737 | | - "cell_type": "markdown", |
738 | | - "metadata": {}, |
739 | | - "source": [ |
740 | | - "<br>\n", |
741 | | - "\n", |
742 | | - "### Micro-Exercise:\n", |
743 | | - "* Do you see more things which could be changed in the titanic data-set here?\n" |
744 | | - ] |
745 | | - }, |
746 | 766 | { |
747 | 767 | "cell_type": "markdown", |
748 | 768 | "metadata": {}, |
|
823 | 843 | "metadata": {}, |
824 | 844 | "outputs": [], |
825 | 845 | "source": [ |
826 | | - "df.loc[ 0:3, : ] # this selects the first 4 rows." |
| 846 | + "df.loc[ 0:3 , : ] # this selects the first 4 rows." |
827 | 847 | ] |
828 | 848 | }, |
829 | 849 | { |
|
832 | 852 | "metadata": {}, |
833 | 853 | "outputs": [], |
834 | 854 | "source": [ |
835 | | - "df.iloc[ 0:3, : ] # this selects the first 3 rows." |
| 855 | + "df.iloc[ 0:3 , : ] # this selects the first 3 rows." |
836 | 856 | ] |
837 | 857 | }, |
838 | 858 | { |
|
1144 | 1164 | "metadata": {}, |
1145 | 1165 | "outputs": [], |
1146 | 1166 | "source": [ |
1147 | | - "df_census = pd.read_table(\"data/swiss_census_1880.csv\", sep=\",\")\n", |
| 1167 | + "df_census = pd.read_table(\"data/swiss_census_1880.csv\", \n", |
| 1168 | + " sep=\",\")\n", |
1148 | 1169 | "df_census.loc[:5, [\"town name\", \"Total\", \"Male\"]]" |
1149 | 1170 | ] |
1150 | 1171 | }, |
|
1212 | 1233 | "outputs": [], |
1213 | 1234 | "source": [ |
1214 | 1235 | "# NA is represented using pd.NA\n", |
1215 | | - "df.loc[ df.Pclass==3 , 'Fare'] = pd.NA\n", |
| 1236 | + "df.loc[ df.Pclass==\"III\" , 'Fare'] = pd.NA\n", |
1216 | 1237 | "df.head()" |
1217 | 1238 | ] |
1218 | 1239 | }, |
|
1513 | 1534 | "metadata": {}, |
1514 | 1535 | "outputs": [], |
1515 | 1536 | "source": [ |
1516 | | - "df = df.drop(columns='discount') # use the 'index' argument to remove rows instead\n", |
| 1537 | + "df.drop(columns='discount' , inplace=True) # use the 'index' argument to remove rows instead\n", |
1517 | 1538 | "print(\"is 'discount' part of the columns : \" , 'discount' in df.columns)" |
1518 | 1539 | ] |
1519 | 1540 | }, |
|
1546 | 1567 | "## inplace = True : df is changed, df2 is None" |
1547 | 1568 | ] |
1548 | 1569 | }, |
| 1570 | + { |
| 1571 | + "cell_type": "code", |
| 1572 | + "execution_count": null, |
| 1573 | + "metadata": {}, |
| 1574 | + "outputs": [], |
| 1575 | + "source": [ |
| 1576 | + "help( df.drop )" |
| 1577 | + ] |
| 1578 | + }, |
1549 | 1579 | { |
1550 | 1580 | "cell_type": "markdown", |
1551 | 1581 | "metadata": {}, |
|
1606 | 1636 | "execution_count": null, |
1607 | 1637 | "metadata": {}, |
1608 | 1638 | "outputs": [], |
1609 | | - "source": [] |
| 1639 | + "source": [ |
| 1640 | + "m_survived = df.Survived == 1\n", |
| 1641 | + "\n", |
| 1642 | + "df.loc[m_survived , 'Sex'].value_counts()\n" |
| 1643 | + ] |
1610 | 1644 | }, |
1611 | 1645 | { |
1612 | 1646 | "cell_type": "markdown", |
|
1632 | 1666 | "outputs": [], |
1633 | 1667 | "source": [ |
1634 | 1668 | "# %load -r 4- solutions/solution_01_01.py\n", |
1635 | | - "#2. Create a new column Title is the DataFrame representing the title by which passengers should be addressed. " |
| 1669 | + "#2. Create a new column Title is the DataFrame representing the title by which passengers should be addressed. The title can be found in the passenger name and is the only word ending with a '.'" |
1636 | 1670 | ] |
1637 | 1671 | }, |
1638 | 1672 | { |
|
1733 | 1767 | "mask_male = df.Sex == 'male'\n", |
1734 | 1768 | "\n", |
1735 | 1769 | "print('median fare of male', df.Fare[mask_male].median() )\n", |
1736 | | - "print('median fare of female', df.Fare[~mask_male].median() ) # note the use of ~ to reverse the mask!" |
| 1770 | + "print('median fare of female', df.Fare[~mask_male].median() )\n", |
| 1771 | + "# note the use of ~ to reverse the mask!" |
1737 | 1772 | ] |
1738 | 1773 | }, |
1739 | 1774 | { |
|
1856 | 1891 | "metadata": {}, |
1857 | 1892 | "outputs": [], |
1858 | 1893 | "source": [ |
1859 | | - "# %load solutions/solution_describe.py" |
| 1894 | + "# %load solutions/solution_describe.py\n", |
| 1895 | + "df.Pclass = df.Pclass.astype('object')\n", |
| 1896 | + "df.Survived = df.Survived.astype('bool')\n", |
| 1897 | + "df.describe()" |
1860 | 1898 | ] |
1861 | 1899 | }, |
1862 | 1900 | { |
|
2015 | 2053 | "axes[0].set_title(\"sine plot\")\n", |
2016 | 2054 | "axes[0].set_xlabel(\"value\")\n", |
2017 | 2055 | "axes[0].set_ylabel(\"sine/cosine value\")\n", |
| 2056 | + "axes[0].legend()\n", |
2018 | 2057 | "\n", |
2019 | 2058 | "## plotting on the second axe == right panel\n", |
2020 | 2059 | "axes[1].plot(x, np.cos(x), label='cos')\n", |
2021 | | - "axes[1].set_title(\"sine plot\")\n", |
| 2060 | + "axes[1].set_title(\"cosine plot\")\n", |
2022 | 2061 | "axes[1].set_xlabel(\"value\")\n", |
2023 | 2062 | "axes[1].set_ylabel(\"sine/cosine value\")\n", |
2024 | 2063 | "\n", |
|
2092 | 2131 | "# then you can specify where each plot goes on the figure with the ax argument of the ([0,0] is the top left corner)\n", |
2093 | 2132 | "\n", |
2094 | 2133 | "# Plot a simple histogram with binsize determined automatically\n", |
2095 | | - "sns.histplot(dfFractions['0-14 y.o.'], kde=False, color=\"b\", ax=axes[0, 0]).set_title('automatic')\n", |
| 2134 | + "sns.histplot(dfFractions['0-14 y.o.'], kde=False, color=\"b\",\n", |
| 2135 | + " ax=axes[0, 0]).set_title('automatic')\n", |
2096 | 2136 | "\n", |
2097 | 2137 | "# Plot a simple histogram with binsize 5, 10 , 1000\n", |
2098 | 2138 | "sns.histplot(dfFractions['0-14 y.o.'], bins=5 , kde=False, color=\"b\", ax=axes[0, 1]).set_title('5 bins')\n", |
|
2310 | 2350 | "* Compute survival rates by gender, age category and passenger class." |
2311 | 2351 | ] |
2312 | 2352 | }, |
| 2353 | + { |
| 2354 | + "cell_type": "code", |
| 2355 | + "execution_count": null, |
| 2356 | + "metadata": {}, |
| 2357 | + "outputs": [], |
| 2358 | + "source": [ |
| 2359 | + "dfc = df.copy()\n", |
| 2360 | + "def age_category(x):\n", |
| 2361 | + " age_classes = {\"child\": 12, \"teenager\": 17, \"adult\": 64, \"senior\": 200}\n", |
| 2362 | + " for label, threshold in age_classes.items():\n", |
| 2363 | + " if x <= threshold:\n", |
| 2364 | + " return label\n" |
| 2365 | + ] |
| 2366 | + }, |
2313 | 2367 | { |
2314 | 2368 | "cell_type": "code", |
2315 | 2369 | "execution_count": null, |
|
2399 | 2453 | "metadata": {}, |
2400 | 2454 | "outputs": [], |
2401 | 2455 | "source": [ |
2402 | | - "sns.catplot( x='Fare' , y = 'Pclass' , orient='horizontal' , data=df , aspect = 2.0 , height = 7)" |
| 2456 | + "sns.catplot( x='Fare' , y = 'Pclass' ,\n", |
| 2457 | + " orient='horizontal' , \n", |
| 2458 | + " data=df , aspect = 2.0 , height = 7)" |
2403 | 2459 | ] |
2404 | 2460 | }, |
2405 | 2461 | { |
|
2475 | 2531 | "metadata": {}, |
2476 | 2532 | "outputs": [], |
2477 | 2533 | "source": [ |
2478 | | - "sns.catplot( x='Fare' , y = 'Pclass' , hue='Sex' , orient='horizontal' , kind='box', data=df , aspect = 2.0 , height = 7)\n", |
| 2534 | + "sns.catplot( x='Fare' , \n", |
| 2535 | + " y = 'Pclass' , \n", |
| 2536 | + " hue='Sex' , \n", |
| 2537 | + " orient='horizontal' , \n", |
| 2538 | + " kind='box', data=df , aspect = 2.0 , height = 4)\n", |
2479 | 2539 | "## it is also very nice with kind='point'" |
2480 | 2540 | ] |
2481 | 2541 | }, |
|
2500 | 2560 | { |
2501 | 2561 | "cell_type": "code", |
2502 | 2562 | "execution_count": null, |
2503 | | - "metadata": {}, |
| 2563 | + "metadata": { |
| 2564 | + "scrolled": true |
| 2565 | + }, |
2504 | 2566 | "outputs": [], |
2505 | 2567 | "source": [ |
2506 | 2568 | "df_census = pd.read_table(\"data/swiss_census_1880.csv\", sep=\",\")\n", |
2507 | 2569 | "df_census.head()" |
2508 | 2570 | ] |
2509 | 2571 | }, |
| 2572 | + { |
| 2573 | + "cell_type": "code", |
| 2574 | + "execution_count": null, |
| 2575 | + "metadata": {}, |
| 2576 | + "outputs": [], |
| 2577 | + "source": [] |
| 2578 | + }, |
2510 | 2579 | { |
2511 | 2580 | "cell_type": "code", |
2512 | 2581 | "execution_count": null, |
|
2541 | 2610 | "outputs": [], |
2542 | 2611 | "source": [ |
2543 | 2612 | "# %load solutions/solution_01_03_fancy.py\n", |
2544 | | - "# fancy solution inspired by https://seaborn.pydata.org/examples/kde_ridgeplot.html\n" |
| 2613 | + "# fancy solution inspired by https://seaborn.pydata.org/examples/kde_ridgeplot.html" |
2545 | 2614 | ] |
2546 | 2615 | }, |
2547 | 2616 | { |
|
2621 | 2690 | "source": [ |
2622 | 2691 | "fig = plt.figure(figsize=(15,10))\n", |
2623 | 2692 | "ax = sns.scatterplot( x = 'Fare' , y = 'Age' , \n", |
2624 | | - " hue='Pclass' , palette=['xkcd:tomato','xkcd:teal','xkcd:mustard'],\n", |
| 2693 | + " hue='Pclass' , \n", |
| 2694 | + " palette=['xkcd:tomato',\n", |
| 2695 | + " 'xkcd:teal','xkcd:mustard'],\n", |
2625 | 2696 | " style='Sex', data=df , s=100 )\n", |
2626 | 2697 | "ax.set(xscale=\"log\") # setting axis to log scale.\n", |
2627 | 2698 | "ax.set_xlim( (3,1000) ) # manually setting the limit of the x axis.\n" |
|
2723 | 2794 | " data=dfFractions , kind = k , orient='h',height=10, aspect=2 )\n", |
2724 | 2795 | "\n", |
2725 | 2796 | "# Save plot to disk, using the savefig() method:\n", |
2726 | | - "my_plot.savefig(\"output.png\")" |
| 2797 | + "# my_plot.savefig(\"output.png\")" |
2727 | 2798 | ] |
2728 | 2799 | }, |
2729 | 2800 | { |
|
0 commit comments