diff --git a/module2/exo4/data.csv b/module2/exo4/data.csv index ac1243ee245ec59c37634add7beb6978e60d3df9..425899b842dc6eb6314ae4a5d0d3b5b020df31e0 100644 --- a/module2/exo4/data.csv +++ b/module2/exo4/data.csv @@ -1,28 +1,29 @@ -01/02/21;14,6460667061335 -02/02/21;73,4672357276549 -03/02/21;44,2291538031916 -04/02/21;71,2903834045354 -05/02/21;68,4296251985456 -06/02/21;63,7415933598351 -07/02/21;12,1364474195974 -08/02/21;31,333298339329 -09/02/21;88,577182629412 -10/02/21;15,7374298284668 -11/02/21;5,76424151805257 -12/02/21;31,553181578714 -13/02/21;2,8429101140257 -14/02/21;110,497435357668 -15/02/21;53,519193819704 -16/02/21;29,0710366548617 -17/02/21;40,9590068501374 -18/02/21;108,901226649965 -19/02/21;89,8347247541098 -20/02/21;104,444763954637 -21/02/21;97,5087720203479 -22/02/21;91,3232707890844 -23/02/21;69,9688805761576 -24/02/21;18,6351020508254 -25/02/21;95,3568454045806 -26/02/21;65,405377470914 -27/02/21;58,6261571771731 -28/02/21;56,492475928146 +2021-02-01;81.8199141820915 +2021-02-02;45.6301076036676 +2021-02-03;70.8706487695057 +2021-02-04;5.97511114188613 +2021-02-05;101.240121644893 +2021-02-06;103.766043900565 +2021-02-07;52.7243273488364 +2021-02-08;68.7124193984776 +2021-02-09;24.7699235269981 +2021-02-10;118.519011934154 +2021-02-11;72.366803329567 +2021-02-12;114.271575633934 +2021-02-13;22.5772258584814 +2021-02-14;9.45448876418252 +2021-02-15;82.0417788209343 +2021-02-16;113.367188637391 +2021-02-17;69.0559521907761 +2021-02-18;23.3930819081197 +2021-02-19;59.4513861286365 +2021-02-20;11.8306198096613 +2021-02-21;38.6294301996271 +2021-02-22;55.8762511123907 +2021-02-23;69.6027586204646 +2021-02-24;12.4944003236117 +2021-02-25;115.595594989872 +2021-02-26;56.1790065946262 +2021-02-27;64.3230348460476 +2021-02-28;4.86203636954475 + diff --git a/module2/exo4/exercice_python_en.org b/module2/exo4/exercice_python_en.org index 960351a96254976e4b167e8ac8c01952636d3295..59d202220a0cc7ac6ed3cc88f71340e639c280de 100644 --- a/module2/exo4/exercice_python_en.org +++ b/module2/exo4/exercice_python_en.org @@ -26,3 +26,146 @@ and minimum value, regarding the time spent on each day. * Results of the experiments + I'm using the [[https://pandas.pydata.org/][Pandas library]] to facilitate reading the date from the + CSV file and to learn a new tool :). + + + +#+begin_src python :results value :session *python* :exports both #using value, prints the variable without showing the console output +import pandas as pd # using pandas to facilitate working with date and time +dataframe = pd.read_csv("data.csv", parse_dates=[0], delimiter = ';', header=None) +dataframe +#+end_src + +#+RESULTS: +#+begin_example + 0 1 +0 2021-02-01 81.819914 +1 2021-02-02 45.630108 +2 2021-02-03 70.870649 +3 2021-02-04 5.975111 +4 2021-02-05 101.240122 +5 2021-02-06 103.766044 +6 2021-02-07 52.724327 +7 2021-02-08 68.712419 +8 2021-02-09 24.769924 +9 2021-02-10 118.519012 +10 2021-02-11 72.366803 +11 2021-02-12 114.271576 +12 2021-02-13 22.577226 +13 2021-02-14 9.454489 +14 2021-02-15 82.041779 +15 2021-02-16 113.367189 +16 2021-02-17 69.055952 +17 2021-02-18 23.393082 +18 2021-02-19 59.451386 +19 2021-02-20 11.830620 +20 2021-02-21 38.629430 +21 2021-02-22 55.876251 +22 2021-02-23 69.602759 +23 2021-02-24 12.494400 +24 2021-02-25 115.595595 +25 2021-02-26 56.179007 +26 2021-02-27 64.323035 +27 2021-02-28 4.862036 +#+end_example + + + +* Calculating the average/mean +We can use pandas' [[https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.mean.html][mean method]] + +#+begin_src python :results output :session *python* :exports both #using output, prints only what is shown in the console +average = dataframe[1].mean() +print(average) +#+end_src + +#+RESULTS: +: 59.621437271033706 + +* Calculating the standard deviation +We can use pandas' [[https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.std.html][std method]] +#+begin_src python :results value :session *python* :exports both +std = dataframe[1].std() +std +#+end_src + +#+RESULTS: +: 36.12909565271962 + + +* Calculating the median +We can use pandas' [[https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.median.html][median method]] +#+begin_src python :results value :session *python* :exports both +median = dataframe[1].median() +median +#+end_src + +#+RESULTS: +: 61.88721048734205 + +* Finding the minimum value (time spent) +We can use pandas' [[https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.min.html][min method]] +#+begin_src python :results value :session *python* :exports both +min = dataframe[1].min() +min +#+end_src + +#+RESULTS: +: 4.86203636954475 + +* Finding the day with the minimum time spent studying +We can use pandas' [[https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.idxmin.html][idxmin method]] +#+begin_src python :results output :session *python* :exports both +idmin = dataframe[1].idxmin() +idmin +print (dataframe[0][idmin] , dataframe[1][idmin] ) +#+end_src + +#+RESULTS: +: 2021-02-28 00:00:00 4.86203636954475 + +* Finding the maximum value (time spent) +We can use pandas' [[https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.max.html][max method]] +#+begin_src python :results value :session *python* :exports both +max = dataframe[1].max() +max +#+end_src + +#+RESULTS: +: 118.519011934154 + +* Finding the day with the maximum time spent studying + +We can use pandas' [[https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.idxmax.html][idxmax method]] +#+begin_src python :results output :session *python* :exports both +idmax = dataframe[1].idxmax() +idmax +print (dataframe[0][idmax],dataframe[1][idmax] ) +#+end_src + +#+RESULTS: +: 2021-02-10 00:00:00 118.519011934154 + +* Generating a graphic of the data: + +#+begin_src python :results output file :session *python* :var matplot_lib_filename2="simple_plot.png" :exports both +from matplotlib import pyplot as plt +fig, ax = plt.subplots(figsize=(12, 12)) + + +ax.bar(dataframe.index.values, + dataframe[1], + color='purple') + + +ax.set(xlabel="Date", + ylabel="Time Spent", + title="Daily Time spent studying for the MOOC on reproducible research - feb/2021") + +plt.savefig(matplot_lib_filename2) +print(matplot_lib_filename2) +#+end_src + +#+RESULTS: +[[file:simple_plot.png]] diff --git a/module2/exo4/simple_plot.png b/module2/exo4/simple_plot.png new file mode 100644 index 0000000000000000000000000000000000000000..6542fc7615a9fa11979545e3f33292811e305045 Binary files /dev/null and b/module2/exo4/simple_plot.png differ