issue157:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue157:python [2020/05/31 09:48] – d52fr | issue157:python [2020/06/06 13:52] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | We are all experiencing a new world due to COVID-19. | + | **We are all experiencing a new world due to COVID-19. |
- | We are presented with the number of confirmed cases, number of deaths and number of hospitalizations due to COVID-19 on every TV news show, radio show and on the Internet. | + | We are presented with the number of confirmed cases, number of deaths and number of hospitalizations due to COVID-19 on every TV news show, radio show and on the Internet. |
- | Way back in December 2018 (FCM#140), I talked about Pandas and Python. | + | Nous faisons tous l' |
+ | |||
+ | À chaque journal télévisé, | ||
+ | |||
+ | **Way back in December 2018 (FCM#140), I talked about Pandas and Python. | ||
To get started, we need some data. I’m going to use a Comma Separated Variable data set available from https:// | To get started, we need some data. I’m going to use a Comma Separated Variable data set available from https:// | ||
Ligne 9: | Ligne 13: | ||
When you get to the page, scroll down below the chart and find the first download “button” that says “time_series_covid19_confirmed_global.csv” to the left of it. This will download the CSV file to your machine. | When you get to the page, scroll down below the chart and find the first download “button” that says “time_series_covid19_confirmed_global.csv” to the left of it. This will download the CSV file to your machine. | ||
- | Now use LibreOffice Calc (or some other spreadsheet viewer) to open the file. In theory, you should be able to simply double click on the downloaded file. Accept the import settings box. | + | Now use LibreOffice Calc (or some other spreadsheet viewer) to open the file. In theory, you should be able to simply double click on the downloaded file. Accept the import settings box.** |
+ | |||
+ | En décembre 2018 (le FCM n° 140), j'ai parlé de Pandas et Python. Ce mois-ci, nous utiliserons Pandas et Python pour regarder certains de ces chiffres et en faire des graphiques. Si Pandas n'est pas installé sur votre machine, retournez dans le FCM n° 140 pour les étapes de l' | ||
+ | |||
+ | Pour commencer, nous avons besoin de quelques données. Je vais utiliser un jeu de données CVS (Comma Separated Variables - Variables séparées par une virgule) disponible sur https:// | ||
+ | |||
+ | Quand vous arrivez sur la page, descendez sous le tableau et trouvez le premier bouton « download » qui indique « time_series_covid19_confirmed_global.csv » à sa gauche. Cela téléchargera le fichier .csv sur votre machine. | ||
+ | |||
+ | Maintenant, utilisez LibreOffice Calc (ou un autre tableur) pour ouvrir le fichier. En théorie, vous ne devriez avoir qu'à double-cliquer sur le fichier téléchargé. Acceptez la fenêtre des paramètres d' | ||
+ | |||
+ | **PLEASE NOTE: I am using data that I downloaded on 5 May, 2020. Yours will be a little bit different, mainly in the fact that it will have more data, column wise as well as possibly more rows (since more regions can be added as new cases spread to more countries). | ||
+ | |||
+ | The important thing to do here is to verify that the Country or Region that you are interested in is somewhere in Column B. It might be partially in Column B and partially in Column A. For example, if you are interested in Scotland, you would use the row marked “United Kingdom”, but if you want Greenland, you need to find “Denmark” in Column B then “Greenland” in Column A. For the purposes of this article, we’ll use “US” which is around row 227 (at least for now).** | ||
- | PLEASE NOTE: I am using data that I downloaded on 5 May, 2020. Yours will be a little bit different, mainly in the fact that it will have more data, column wise as well as possibly more rows (since more regions can be added as new cases spread to more countries). | + | MERCI DE NOTER : J' |
- | The important thing to do here is to verify that the Country or Region that you are interested in is somewhere in Column | + | La chose importante à faire ici est de vérifier que le pays ou la région qui vous intéresse est quelque part dans la colonne |
- | Create a convenient folder somewhere, copy the CSV file into the folder and open a terminal window in that folder. | + | **Create a convenient folder somewhere, copy the CSV file into the folder and open a terminal window in that folder. |
Now it’s time to do some coding. | Now it’s time to do some coding. | ||
- | Look at the last line of the pandas head/tail dump. It says that there are 266 rows and 108 columns. | + | Look at the last line of the pandas head/tail dump. It says that there are 266 rows and 108 columns. |
- | Again, here we want to verify a few things. | + | Créez un dossier quelque part à votre guise, copiez-y le fichier CSV et ouvrez une fenêtre de terminal dans ce dossier. (J' |
+ | |||
+ | Il est temps maintenant de faire un peu de codage. Nous utiliserons l’interpréteur Python pour cet exemple. Voyez l' | ||
+ | |||
+ | Regardez le dernière ligne de l' | ||
+ | |||
+ | **Again, here we want to verify a few things. | ||
Now we have some data that we can play with. BUT, we need to get a bit more information to make our programming easier. | Now we have some data that we can play with. BUT, we need to get a bit more information to make our programming easier. | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | (266, 108) | ||
+ | >>> | ||
+ | >>> | ||
+ | 108 | ||
+ | >>> | ||
+ | |||
+ | À nouveau, nous voulons vérifier quelques points. D' | ||
+ | |||
+ | Maintenant, nous avons des données avec lesquelles jouer. Mais nous avons besoin d'un peu plus d' | ||
>>> | >>> | ||
Ligne 33: | Ligne 67: | ||
>>> | >>> | ||
- | Here we use data.shape() to get the number of rows and the number of columns in the dataframe. | + | **Here we use data.shape() to get the number of rows and the number of columns in the dataframe. |
Now (top right) we will grab just the columns that contain the confirmed number of cases (Column E or 4 (again zero based)) through lastcol (108) for Row 255. We’ll use the .iloc method to grab the row, start column and last column from the dataframe and assign it to variable s1a. | Now (top right) we will grab just the columns that contain the confirmed number of cases (Column E or 4 (again zero based)) through lastcol (108) for Row 255. We’ll use the .iloc method to grab the row, start column and last column from the dataframe and assign it to variable s1a. | ||
- | So we now have data that we can use, but the data was extracted, it came back as a data series, not a data frame. | + | So we now have data that we can use, but the data was extracted, it came back as a data series, not a data frame. |
- | So now we’ve got data that we can almost play with. But first, let’s assign the column headings to some proper, meaningful information. | + | Ici, nous utilisons data.shape() pour obtenir le nombre de lignes et le nombre de colonnes du jeu de données. Cela est retourné comme un tuple ; aussi, nous pouvons assigner une variable « lastcol » aux 108 (sh[1]) de la variable sh. |
+ | |||
+ | Maintenant (en haut à droite), nous ne récupérerons que les colonnes qui contiennent le nombre de cas confirmés (Colonne E ou 4 - attention, début à zéro) via lastcol (108) pour la ligne 225. Nous utiliserons la méthode .iloc pour récupérer la ligne, la première et la dernière colonnes du jeu de données et les assigner à la variable sla. | ||
+ | |||
+ | Ainsi, nous avons maintenant des données que nous pouvons utiliser ; mais les données ont été extraites, sous la forme d'une série de données, pas d'un jeu de données. Aussi, nous devons les convertir en jeu de données. Voir en bas à droite. | ||
+ | |||
+ | **So now we’ve got data that we can almost play with. But first, let’s assign the column headings to some proper, meaningful information. | ||
It’s a very busy plot (bottom right), but you can definitely see the same kind of data you do from the news. | It’s a very busy plot (bottom right), but you can definitely see the same kind of data you do from the news. | ||
Ligne 46: | Ligne 86: | ||
Now let’s see the daily differences on a graph... | Now let’s see the daily differences on a graph... | ||
+ | |||
+ | >>> | ||
+ | |||
+ | < | ||
+ | |||
+ | >>> | ||
+ | |||
+ | Ainsi, nous avons maintenant des données avec lesquelles jouer, ou presque. Mais d' | ||
+ | |||
+ | C'est un tracé très chargé (en bas à droite), mais vous pouvez vraiment voir le même genre de données que celles qui paraissent dans les journaux. | ||
+ | |||
+ | Pour la partie suivante de notre examen des données, nous devons calculer le nombre de nouveaux cas d'un jour sur l' | ||
+ | |||
+ | Maintenant, faisons apparaître les écarts quotidiens sur un graphique : | ||
>>> | >>> | ||
Ligne 53: | Ligne 107: | ||
>>> | >>> | ||
- | Now that you have the basics of dealing with the basic data, you can go back to the beginning, where we pulled the data for the US and change it to the country or region of your choice. | + | **Now that you have the basics of dealing with the basic data, you can go back to the beginning, where we pulled the data for the US and change it to the country or region of your choice. |
s1 = data.loc[data[' | s1 = data.loc[data[' | ||
Ligne 65: | Ligne 119: | ||
You would change it to... | You would change it to... | ||
+ | |||
+ | s1a = data.iloc[175, | ||
+ | |||
+ | Maintenant que nous avons les bases pour traiter les données simples, vous pouvez retourner au début, là où nous avons extrait les données pour les USA et changer pour le pays ou la région de votre choix. Par exemple, modifiez la ligne : | ||
+ | |||
+ | s1 = data.loc[data[' | ||
+ | |||
+ | en… | ||
+ | |||
+ | s1 = data.loc[data[' | ||
+ | |||
+ | Si vous imprimez les données de s1, vous verrez que la ligne de la Norvège est la 175. Aussi, avec la ligne suivante, nous n' | ||
+ | |||
+ | s1a = data.iloc[225, | ||
+ | |||
+ | vous pouvez la modifier en : | ||
s1a = data.iloc[175, | s1a = data.iloc[175, | ||
- | At this point, you would repeat all of the other steps again to create and modify the dataframe so you can plot it. | + | **At this point, you would repeat all of the other steps again to create and modify the dataframe so you can plot it. |
- | What exactly should we take away from this data? That’s a very good question. | + | What exactly should we take away from this data? That’s a very good question. |
- | WIth a little bit of creative web searches, you can find a lot more information on Pandas, various datasets and types of plots and options that you can use to show your data. | + | À ce stade, vous pourriez répéter toutes les étapes précédentes pour créer et modifier les jeux de données de façon à les tracer. |
+ | Que devons-nous tirer vraiment de ces données ? C'est une très bonne question. La précision de celles-ci est actuellement une question (de séries) en elle-même. Il y a des spéculations qui suggèrent que le nombre de cas confirmés est bas dans beaucoup de régions du fait du manque de tests sur la population. Vous ne pouvez jamais être sûr des données, sauf si vous les regroupez vous-même. Dans des cas comme celui-ci, votre seul choix est de croire, avec un petit grain de sel, que les données ont été relevées avec le plus grand soin. | ||
- | Until next month; stay safe, healthy, positive | + | **WIth a little bit of creative web searches, you can find a lot more information on Pandas, various datasets |
- | Lignes noires de l' | + | |
+ | Until next month; stay safe, healthy, positive and creative!** | ||
+ | |||
+ | Avec un peu de créativité dans vos recherches sur le Web, vous pouvez trouver beaucoup plus d' | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | |||
+ | //Lignes noires de l' | ||
**Now, we need to import two libraries, pandas and matplotlib.pyplot. | **Now, we need to import two libraries, pandas and matplotlib.pyplot. | ||
+ | Maintenant, nous devons importer deux bibliothèques, | ||
**Now, let’s set the filename of the .csv file into a variable…** | **Now, let’s set the filename of the .csv file into a variable…** | ||
+ | |||
+ | Maintenant, mettons le nom du fichier .csv dans une variable : | ||
**Next, have Pandas read the spreadsheet into a dataframe…** | **Next, have Pandas read the spreadsheet into a dataframe…** | ||
- | Lignes noires de l' | + | Puis, faisons en sorte que Pandas lise le tableur vers le jeu de données : |
+ | |||
+ | //Lignes noires de l' | ||
**Now we can plot the data. Remember, there are 104 data points, so the date information on the X axis will be pretty squished together.** | **Now we can plot the data. Remember, there are 104 data points, so the date information on the X axis will be pretty squished together.** | ||
+ | |||
+ | Maintenant, nous pouvons tracer les données. Souvenez-vous qu'il y a 104 points ; aussi, les informations des dates sur l'axe des X seront bien tassées. |
issue157/python.1590911287.txt.gz · Dernière modification : 2020/05/31 09:48 de d52fr