|
250 | 250 | "surveys_df.loc[[0, 10], :]" |
251 | 251 | ] |
252 | 252 | }, |
253 | | - { |
254 | | - "cell_type": "code", |
255 | | - "execution_count": null, |
256 | | - "id": "2e2d84c1-c559-4302-94ff-ff1a31fcf43b", |
257 | | - "metadata": { |
258 | | - "lang": "fr", |
259 | | - "tags": [ |
260 | | - "soln" |
261 | | - ] |
262 | | - }, |
263 | | - "outputs": [], |
264 | | - "source": [ |
265 | | - "# Erreur 1 - Qu'arrive-t-il si on exécute le code ci-dessous?\n", |
266 | | - "surveys_df.loc[[0, 10, 35548], :]" |
267 | | - ] |
268 | | - }, |
269 | | - { |
270 | | - "cell_type": "code", |
271 | | - "execution_count": null, |
272 | | - "id": "384110f3-b095-48f5-85bc-646a8f7ea029", |
273 | | - "metadata": { |
274 | | - "lang": "fr", |
275 | | - "tags": [ |
276 | | - "exer" |
277 | | - ] |
278 | | - }, |
279 | | - "outputs": [], |
280 | | - "source": [ |
281 | | - "# Erreur 1 - Qu'arrive-t-il si on exécute le code ci-dessous?\n", |
282 | | - "surveys_df.loc[[0, 10, 35549], :]" |
283 | | - ] |
284 | | - }, |
285 | | - { |
286 | | - "cell_type": "code", |
287 | | - "execution_count": null, |
288 | | - "id": "20a4e3b6-56e1-4ba2-a177-9c273ff08e9b", |
289 | | - "metadata": { |
290 | | - "lang": "en", |
291 | | - "tags": [ |
292 | | - "soln" |
293 | | - ] |
294 | | - }, |
295 | | - "outputs": [], |
296 | | - "source": [ |
297 | | - "# Error 1 - What happens when you type the code below?\n", |
298 | | - "surveys_df.loc[[0, 10, 35548], :]" |
299 | | - ] |
300 | | - }, |
301 | | - { |
302 | | - "cell_type": "code", |
303 | | - "execution_count": null, |
304 | | - "id": "7643732e-f87e-4be8-90d7-6b40d139c32c", |
305 | | - "metadata": { |
306 | | - "lang": "en", |
307 | | - "tags": [ |
308 | | - "exer" |
309 | | - ] |
310 | | - }, |
311 | | - "outputs": [], |
312 | | - "source": [ |
313 | | - "# Error 1 - What happens when you type the code below?\n", |
314 | | - "surveys_df.loc[[0, 10, 35549], :]" |
315 | | - ] |
316 | | - }, |
317 | 253 | { |
318 | 254 | "cell_type": "code", |
319 | 255 | "execution_count": null, |
|
326 | 262 | }, |
327 | 263 | "outputs": [], |
328 | 264 | "source": [ |
329 | | - "# Erreur 2 - Qu'arrive-t-il si on exécute le code ci-dessous?\n", |
| 265 | + "# Erreur - Qu'arrive-t-il si on exécute le code ci-dessous?\n", |
330 | 266 | "surveys_df.loc[0:4, 'month':'plot_id']" |
331 | 267 | ] |
332 | 268 | }, |
|
342 | 278 | }, |
343 | 279 | "outputs": [], |
344 | 280 | "source": [ |
345 | | - "# Erreur 2 - Qu'arrive-t-il si on exécute le code ci-dessous?\n", |
| 281 | + "# Erreur - Qu'arrive-t-il si on exécute le code ci-dessous?\n", |
346 | 282 | "surveys_df.loc[0:4, 1:4] # 'month':'plot_id'" |
347 | 283 | ] |
348 | 284 | }, |
|
358 | 294 | }, |
359 | 295 | "outputs": [], |
360 | 296 | "source": [ |
361 | | - "# Error 2 - What happens when you type the code below?\n", |
| 297 | + "# Error - What happens when you type the code below?\n", |
362 | 298 | "surveys_df.loc[0:4, 'month':'plot_id']" |
363 | 299 | ] |
364 | 300 | }, |
|
374 | 310 | }, |
375 | 311 | "outputs": [], |
376 | 312 | "source": [ |
377 | | - "# Error 2 - What happens when you type the code below?\n", |
| 313 | + "# Error - What happens when you type the code below?\n", |
378 | 314 | "surveys_df.loc[0:4, 1:4] # 'month':'plot_id'" |
379 | 315 | ] |
380 | 316 | }, |
|
387 | 323 | "source": [ |
388 | 324 | "### Exercice - Une sélection de données\n", |
389 | 325 | "Obtenez la moyenne des **longueurs d'arrière-pieds** et\n", |
390 | | - "des **poids** de chaque **espèce** de `'NL'` à `'PB'` :\n", |
| 326 | + "des **poids** pour les **espèces** `'NL'` et `'PB'` :\n", |
391 | 327 | "\n", |
392 | 328 | "1. Commencez par sélectionner uniquement les colonnes requises.\n", |
393 | | - "2. À la fin, sélectionnez les lignes de `'NL'` à `'PB'`.\n", |
| 329 | + "2. À la fin, sélectionnez les lignes `'NL'` et `'PB'`.\n", |
394 | 330 | "\n", |
395 | 331 | "(3 min.)" |
396 | 332 | ] |
|
404 | 340 | "source": [ |
405 | 341 | "### Exercise - Selecting data\n", |
406 | 342 | "Get the average **hindfoot lengths** and\n", |
407 | | - "**weights** of each **species** from `'NL'` to `'PB'`:\n", |
| 343 | + "**weights** for the **species** `'NL'` and `'PB'`:\n", |
408 | 344 | "\n", |
409 | 345 | "1. Start by selecting only the required columns.\n", |
410 | | - "2. At the end, select the rows from `'NL'` to `'PB'`.\n", |
| 346 | + "2. At the end, select the rows for `'NL'` and `'PB'`.\n", |
411 | 347 | "\n", |
412 | 348 | "(3 min.)" |
413 | 349 | ] |
|
425 | 361 | "outputs": [], |
426 | 362 | "source": [ |
427 | 363 | "colonnes = ['species_id', 'hindfoot_length', 'weight']\n", |
428 | | - "surveys_df[colonnes].groupby('species_id').mean().loc['NL':'PB', :]" |
| 364 | + "surveys_df[colonnes].groupby('species_id').mean().loc[['NL', 'PB'], :]" |
429 | 365 | ] |
430 | 366 | }, |
431 | 367 | { |
|
457 | 393 | "outputs": [], |
458 | 394 | "source": [ |
459 | 395 | "columns = ['species_id', 'hindfoot_length', 'weight']\n", |
460 | | - "surveys_df[columns].groupby('species_id').mean().loc['NL':'PB', :]" |
| 396 | + "surveys_df[columns].groupby('species_id').mean().loc[['NL', 'PB'], :]" |
461 | 397 | ] |
462 | 398 | }, |
463 | 399 | { |
|
658 | 594 | " (surveys_df['year'] == 1991) |\n", |
659 | 595 | " (surveys_df['year'] == 1996) |\n", |
660 | 596 | " (surveys_df['year'] == 2001)\n", |
661 | | - "].shape" |
| 597 | + "]" |
662 | 598 | ] |
663 | 599 | }, |
664 | 600 | { |
|
675 | 611 | " (surveys_df['year'] == 1991) |\n", |
676 | 612 | " (surveys_df['year'] == 1996) |\n", |
677 | 613 | " (surveys_df['year'] == 2001)\n", |
678 | | - "].shape" |
| 614 | + "]" |
679 | 615 | ] |
680 | 616 | }, |
681 | 617 | { |
|
690 | 626 | "# Sélection de trois années avec isin()\n", |
691 | 627 | "surveys_df[\n", |
692 | 628 | " surveys_df['year'].isin([1991, 1996, 2001])\n", |
693 | | - "].shape" |
| 629 | + "]" |
694 | 630 | ] |
695 | 631 | }, |
696 | 632 | { |
|
705 | 641 | "# Selection of three years with isin()\n", |
706 | 642 | "surveys_df[\n", |
707 | 643 | " surveys_df['year'].isin([1991, 1996, 2001])\n", |
708 | | - "].shape" |
| 644 | + "]" |
709 | 645 | ] |
710 | 646 | }, |
711 | 647 | { |
|
870 | 806 | }, |
871 | 807 | "source": [ |
872 | 808 | "`2`. Calculez la moyenne des poids\n", |
873 | | - "selon le site (`plot_id`) et le sexe :\n", |
| 809 | + "selon l'espèce (`species_id`) et le sexe :\n", |
874 | 810 | "* Créez une sélection contenant seulement :\n", |
875 | 811 | " * Les observations ayant une valeur de sexe `F` ou `M`\n", |
876 | 812 | " et ayant un poids supérieur à `0`;\n", |
877 | | - " * Les colonnes de poids, de site et de sexe.\n", |
| 813 | + " * Les colonnes de poids, d'espèce et de sexe.\n", |
878 | 814 | "* Groupez les données et calculez les moyennes de poids.\n", |
879 | 815 | "\n", |
880 | 816 | "(5 min.)" |
|
887 | 823 | "lang": "en" |
888 | 824 | }, |
889 | 825 | "source": [ |
890 | | - "`2`. Get the average weight by site (`plot_id`) and sex:\n", |
| 826 | + "`2`. Get the average weight by species (`species_id`) and sex:\n", |
891 | 827 | "* Create a selection that contains only:\n", |
892 | 828 | " * The observations that are of sex `F` or `M`\n", |
893 | 829 | " and where weight values are greater than `0`;\n", |
894 | | - " * The weight, the site and the sex columns.\n", |
| 830 | + " * The weight, the species and the sex columns.\n", |
895 | 831 | "* Group the data and compute the average weights.\n", |
896 | 832 | "\n", |
897 | 833 | "(5 min.)" |
|
912 | 848 | "# Sélection des enregistrements et des colonnes nécessaires\n", |
913 | 849 | "cond_sexe = surveys_df['sex'].isin(['F', 'M'])\n", |
914 | 850 | "cond_poids = surveys_df['weight'] > 0\n", |
915 | | - "colonnes = ['weight', 'plot_id', 'sex']\n", |
| 851 | + "colonnes = ['weight', 'species_id', 'sex']\n", |
916 | 852 | "\n", |
917 | 853 | "selection = surveys_df.loc[cond_sexe & cond_poids, colonnes]\n", |
918 | 854 | "selection.tail()" |
|
933 | 869 | "# Sélection des enregistrements et des colonnes nécessaires\n", |
934 | 870 | "cond_sexe = surveys_df['sex'].isin(['F', 'M'])\n", |
935 | 871 | "cond_poids = surveys_df['weight'] ###\n", |
936 | | - "colonnes = ['weight', 'plot_id', 'sex']\n", |
| 872 | + "colonnes = ['weight', 'species_id', 'sex']\n", |
937 | 873 | "\n", |
938 | 874 | "selection = surveys_df###\n", |
939 | 875 | "selection.tail()" |
|
954 | 890 | "# Selection of the data with isin()\n", |
955 | 891 | "sex_mask = surveys_df['sex'].isin(['F', 'M'])\n", |
956 | 892 | "weight_mask = surveys_df['weight'] > 0\n", |
957 | | - "columns = ['weight', 'plot_id', 'sex']\n", |
| 893 | + "columns = ['weight', 'species_id', 'sex']\n", |
958 | 894 | "\n", |
959 | 895 | "selection = surveys_df.loc[sex_mask & weight_mask, columns]\n", |
960 | 896 | "selection.tail()" |
|
975 | 911 | "# Selection of the data with isin()\n", |
976 | 912 | "sex_mask = surveys_df['sex'].isin(['F', 'M'])\n", |
977 | 913 | "weight_mask = surveys_df['weight'] ###\n", |
978 | | - "columns = ['weight', 'plot_id', 'sex']\n", |
| 914 | + "columns = ['weight', 'species_id', 'sex']\n", |
979 | 915 | "\n", |
980 | 916 | "selection = surveys_df###\n", |
981 | 917 | "selection.tail()" |
|
993 | 929 | }, |
994 | 930 | "outputs": [], |
995 | 931 | "source": [ |
996 | | - "# Grouper selon les sites et les sexes, calculer les moyennes\n", |
997 | | - "moy_par_site_sexe = selection.groupby(['plot_id', 'sex']).mean()\n", |
998 | | - "moy_par_site_sexe.tail()" |
| 932 | + "# Grouper selon les espèces et les sexes, calculer les moyennes\n", |
| 933 | + "moy_par_espece_sexe = selection.groupby(['species_id', 'sex']).mean()\n", |
| 934 | + "moy_par_espece_sexe.tail()" |
999 | 935 | ] |
1000 | 936 | }, |
1001 | 937 | { |
|
1010 | 946 | }, |
1011 | 947 | "outputs": [], |
1012 | 948 | "source": [ |
1013 | | - "# Grouper selon les sites et les sexes, calculer les moyennes\n", |
1014 | | - "moy_par_site_sexe = selection###\n", |
1015 | | - "moy_par_site_sexe.tail()" |
| 949 | + "# Grouper selon les espèces et les sexes, calculer les moyennes\n", |
| 950 | + "moy_par_espece_sexe = ###.groupby(['species_id', 'sex']).mean()\n", |
| 951 | + "moy_par_espece_sexe.tail()" |
1016 | 952 | ] |
1017 | 953 | }, |
1018 | 954 | { |
|
1027 | 963 | }, |
1028 | 964 | "outputs": [], |
1029 | 965 | "source": [ |
1030 | | - "# Calculate the mean weight for each plot_id and sex combination: \n", |
1031 | | - "avg_by_site_sex = selection.groupby(['plot_id', 'sex']).mean()\n", |
1032 | | - "avg_by_site_sex.tail()" |
| 966 | + "# Calculate the mean weight for each species_id and sex combination\n", |
| 967 | + "avg_by_species_sex = selection.groupby(['species_id', 'sex']).mean()\n", |
| 968 | + "avg_by_species_sex.tail()" |
1033 | 969 | ] |
1034 | 970 | }, |
1035 | 971 | { |
|
1044 | 980 | }, |
1045 | 981 | "outputs": [], |
1046 | 982 | "source": [ |
1047 | | - "# Calculate the mean weight for each plot_id and sex combination: \n", |
1048 | | - "avg_by_site_sex = selection###\n", |
1049 | | - "avg_by_site_sex.tail()" |
| 983 | + "# Calculate the mean weight for each species_id and sex combination\n", |
| 984 | + "avg_by_species_sex = ###.groupby(['species_id', 'sex']).mean()\n", |
| 985 | + "avg_by_species_sex.tail()" |
1050 | 986 | ] |
1051 | 987 | }, |
1052 | 988 | { |
|
0 commit comments