issue141: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 | ||
issue141:python [2019/01/28 08:46] – d52fr | issue141:python [2019/02/08 16:06] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 6: | Ligne 6: | ||
Once you have that downloaded, create a folder to hold the code and the CSV file. This could all be done within the Python shell, but creating a file will make it easier to deal with until you get familiar with the commands and concepts that we will be using. We'll be building the code file step by step as we go along. I've put the source code up on PasteBin at https:// | Once you have that downloaded, create a folder to hold the code and the CSV file. This could all be done within the Python shell, but creating a file will make it easier to deal with until you get familiar with the commands and concepts that we will be using. We'll be building the code file step by step as we go along. I've put the source code up on PasteBin at https:// | ||
+ | |||
+ | Pandas = câlins ... données ? | ||
+ | |||
+ | Cette fois, nous nous concentrerons sur les DataFrames de Pandas, en traitant un scénario du monde semi-réel. | ||
+ | |||
+ | Nous aurons besoin de télécharger un fichier CSV depuis kaggle.com. Le lien est https:// | ||
+ | |||
+ | Une fois que vous l' | ||
**The data that the CSV file holds is really rather simple. There are just four columns... | **The data that the CSV file holds is really rather simple. There are just four columns... | ||
Ligne 26: | Ligne 34: | ||
Of course, this is just the first 8 lines from the file.** | Of course, this is just the first 8 lines from the file.** | ||
+ | |||
+ | Les données contenues dans le fichier CSV sont plutôt simples. Il n'y a que quatre colonnes... | ||
+ | • Date | ||
+ | • Time (l' | ||
+ | • Transaction (un nombre) | ||
+ | • Item (l' | ||
+ | et 21 293 lignes. | ||
+ | |||
+ | Pour commencer, nous créerons une DataFrame en important les données du fichier CSV. Vous pouvez aussi créer des DataFrames à partir d'une table de bases de données, mais, ce sera un article pour une autre fois. Voici un échantillon de ce à quoi ressemble le fichier CSV... | ||
+ | |||
+ | Date, | ||
+ | 2016-10-30, | ||
+ | 2016-10-30, | ||
+ | 2016-10-30, | ||
+ | 2016-10-30, | ||
+ | 2016-10-30, | ||
+ | 2016-10-30, | ||
+ | 2016-10-30, | ||
+ | |||
+ | Bien sûr, ce ne sont que les 8 premières lignes du fichier. | ||
+ | |||
**To get started, we'll import Pandas (just like we did last month), define the filename of the CSV file, and create the DataFrame from the CSV file. | **To get started, we'll import Pandas (just like we did last month), define the filename of the CSV file, and create the DataFrame from the CSV file. | ||
Ligne 42: | Ligne 71: | ||
Now, to do any kind of work with the data, you will need to know the names of the columns. Many times, when we are working, we either don't have time or don't take the time to make notes carefully. This is especially true when we deal with really large data files with more than 50 columns. That can take more time than we have. Thankfully, Pandas has a simple command that we can use to get all of our column headers. The command is ' | Now, to do any kind of work with the data, you will need to know the names of the columns. Many times, when we are working, we either don't have time or don't take the time to make notes carefully. This is especially true when we deal with really large data files with more than 50 columns. That can take more time than we have. Thankfully, Pandas has a simple command that we can use to get all of our column headers. The command is ' | ||
+ | |||
+ | Pour commencer, nous importerons Pandas (exactement comme nous l' | ||
+ | |||
+ | import pandas as pd | ||
+ | |||
+ | filename = ' | ||
+ | |||
+ | df = pd.read_csv(filename) | ||
+ | |||
+ | print(df) | ||
+ | |||
+ | Ce que nous verrons ressemblera aux données montrées ci-dessus. | ||
+ | |||
+ | Toutes les données sont bien là, mais Pandas ne montre qu'une partie des informations de la DataFrame. | ||
+ | |||
+ | Maintenant, quel que soit le travail à faire sur les données, vous aurez besoin de connaître le nom des colonnes. Il arrive très souvent, quand nous travaillons, | ||
**# get and display a list of the column names (headers) | **# get and display a list of the column names (headers) | ||
Ligne 62: | Ligne 107: | ||
Item 21293 | Item 21293 | ||
dtype: int64** | dtype: int64** | ||
+ | |||
+ | # get and display a list of the column names (headers) | ||
+ | |||
+ | col_list = df.columns.values.tolist() | ||
+ | |||
+ | print(col_list) | ||
+ | |||
+ | ce qui nous donnera... | ||
+ | |||
+ | [' | ||
+ | |||
+ | nous pouvons également tout simplement appeler « df.count() » et quelque chose comme ceci nous sera présenté : | ||
+ | |||
+ | print(df.count()) | ||
+ | |||
+ | Date 21293 | ||
+ | Time 21293 | ||
+ | Transaction | ||
+ | Item 21293 | ||
+ | dtype: int64 | ||
**So now, we have created and loaded our DataFrame and we know the column headers and know the number of data rows that we have. Now, let's see how many individual dates we are dealing with. To do that, we can create a list (almost like we did to get the column header list) by using the following command… | **So now, we have created and loaded our DataFrame and we know the column headers and know the number of data rows that we have. Now, let's see how many individual dates we are dealing with. To do that, we can create a list (almost like we did to get the column header list) by using the following command… | ||
Ligne 75: | Ligne 140: | ||
Keep the datelist variable in mind for later on.** | Keep the datelist variable in mind for later on.** | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | datelist = df[' | ||
+ | |||
+ | Ensuite, nous pouvons imprimer la longueur de la liste pour savoir combien elle possède de dates uniques. Nous pouvons aussi inclure les dates la plus proche et la plus lointaine pour lesquelles nous avons des données : | ||
+ | |||
+ | print(len(datelist), | ||
+ | |||
+ | dont les résultats sont : | ||
+ | |||
+ | 159 2016-10-30 2017-04-09 | ||
+ | |||
+ | Gardez en tête la variable datelist pour plus tard. | ||
+ | |||
**We also know that we have the ' | **We also know that we have the ' | ||
Ligne 89: | Ligne 169: | ||
Ok, so now we know that we have a DataFrame that has 4 data columns, the data has 159 unique dates between 2016-10-30 and 2017-04-09, and 95 unique items in the DataFrame, and all in less than 20 lines of code and about 5 minutes of actual work.** | Ok, so now we know that we have a DataFrame that has 4 data columns, the data has 159 unique dates between 2016-10-30 and 2017-04-09, and 95 unique items in the DataFrame, and all in less than 20 lines of code and about 5 minutes of actual work.** | ||
+ | |||
+ | Nous savons aussi que la colonne « Item » existe. Nous pouvons faire la même chose pour voir combien d' | ||
+ | |||
+ | itemlist = df[' | ||
+ | |||
+ | print(itemlist) | ||
+ | |||
+ | print(len(itemlist)) | ||
+ | |||
+ | Je n' | ||
+ | |||
+ | [' | ||
+ | |||
+ | Bon. Maintenant nous savons que nous avons une DataFrame qui possède 4 colonnes, que les données ont 159 dates uniques entre le 30-10-2016 et le 09-04-2017, et qu'il y a 95 articles uniques dans la DataFrame ; et tout cela, avec moins de 20 lignes de code et environ cinq minutes de travail réel. | ||
**Now, before we go any further, it would be a good idea to think about some of the questions that would be asked about our data… probably by the boss. Some of them might be... | **Now, before we go any further, it would be a good idea to think about some of the questions that would be asked about our data… probably by the boss. Some of them might be... | ||
Ligne 100: | Ligne 194: | ||
We know that our data is broken down by date, time of each sale (transaction) and each item sold. In addition, each sale has a unique transaction number that is duplicated if there were multiple items in that sale. For example, let's look at two sales (shown above).** | We know that our data is broken down by date, time of each sale (transaction) and each item sold. In addition, each sale has a unique transaction number that is duplicated if there were multiple items in that sale. For example, let's look at two sales (shown above).** | ||
+ | |||
+ | Avant d' | ||
+ | • Par jour, combien d' | ||
+ | • Par article, quels ont été les plus vendus ? Quels sont les 10 moins bien vendus ? | ||
+ | • Par jour, quels sont les périodes les plus actives ? | ||
+ | |||
+ | Avant de pouvoir répondre à ces questions, nous devons monter un plan pour chacune. Aussi, commençons avec la question n°1. | ||
+ | |||
+ | Par jour, combien d' | ||
+ | |||
+ | Nous savons que les données sont organisées avec la date et l' | ||
**Sale #1 (transaction 1954) was completed on 2016-11-24 at 10:18:24, and was for three items, two bread items and one coffee. | **Sale #1 (transaction 1954) was completed on 2016-11-24 at 10:18:24, and was for three items, two bread items and one coffee. | ||
Ligne 112: | Ligne 217: | ||
| Coffee | | Coffee | ||
| Alfajores | 1** | | Alfajores | 1** | ||
+ | |||
+ | La vente n°1 (transaction 1954) a été terminée le 24-11-2016 à 10:18:24, et comprenait trois articles, deux articles boulangers et un café. | ||
+ | |||
+ | La vente n°2 (transaction 1955) s'est terminée le même jour à 10:23:10, pour deux articles. | ||
+ | |||
+ | Aussi, comment pourrions-nous structurer notre recherche pour accomplir cette tâche ? Si je ne regardais qu'un seul jour, je prendrais tous les enregistrements du jour dit et trierais les enregistrements par articles vendus. Ensuite, je compterais chaque article unique qui a été vendu ce jour-là. En utilisant les cinq enregistrements ci-dessus, ça ressemblerait à quelque chose comme ceci : | ||
+ | |||
+ | Date | ||
+ | -----------|-----------|----- | ||
+ | 2016-11-24 | Bread | 2 | ||
+ | | Coffee | ||
+ | | Alfajores | 1 | ||
**Or to put it another way, I'd group the records by Date, then by Item and count (and record) each occurance of the unique item. | **Or to put it another way, I'd group the records by Date, then by Item and count (and record) each occurance of the unique item. | ||
Ligne 126: | Ligne 243: | ||
#2 - By item, what were the top 10 sellers? What were the bottom 10?** | #2 - By item, what were the top 10 sellers? What were the bottom 10?** | ||
+ | |||
+ | Ou, pour le présenter différemment, | ||
+ | |||
+ | Aussi, comment, à partir de la sortie d'un jeu simple d' | ||
+ | |||
+ | N° 1 - Par Date, montrer combien il a été vendu de chaque article... | ||
+ | |||
+ | Ce n° 1 produit un objet de données Series. | ||
+ | |||
+ | byDate = df.groupby([' | ||
+ | |||
+ | Ainsi, nous savons maintenant comment obtenir les informations pour le patron pour la question n°1. Et pour la question n° 2 : | ||
+ | |||
+ | N° 2 - Par Item, quels sont ceux les plus vendus ? Et les 10 moins bien vendus ? | ||
**Again, we want to find the top 10 sellers as well as the bottom 10. Here we want to groupby Item, counting each Transaction number within each group. Then we want to make sure the items are sorted from hi to low. The .head() and .tail helper routines will give us the answers we need. | **Again, we want to find the top 10 sellers as well as the bottom 10. Here we want to groupby Item, counting each Transaction number within each group. Then we want to make sure the items are sorted from hi to low. The .head() and .tail helper routines will give us the answers we need. | ||
Ligne 140: | Ligne 271: | ||
#3 - By day, what were the busiest times?** | #3 - By day, what were the busiest times?** | ||
+ | |||
+ | À nouveau, nous voulons trouver les 10 articles les mieux vendus tout comme les 10 moins bien vendus. Ici, nous voulons regrouper les articles, en comptant le nombre de transactions pour chaque regroupement. Ensuite, nous voulons nous assurer que les articles sont classés du haut vers le bas. Les routines auxiliaires .head() et .tail() nous donneront les réponses dont nous avons besoin. | ||
+ | |||
+ | # By item, what were the top 10 sellers? What were the bottom 10? | ||
+ | |||
+ | sorteditemcount2 = df.groupby(' | ||
+ | |||
+ | print(sorteditemcount2) | ||
+ | |||
+ | print(sorteditemcount2.head(10)) | ||
+ | |||
+ | print(sorteditemcount2.tail(10)) | ||
+ | |||
+ | N° 3 - Par jour, quelles ont été les périodes les plus actives ? | ||
**Once again, we can group by data and time, then count the number of transaction items. | **Once again, we can group by data and time, then count the number of transaction items. | ||
Ligne 163: | Ligne 308: | ||
[9531 rows x 1 columns]** | [9531 rows x 1 columns]** | ||
+ | |||
+ | Une fois encore, nous pouvons regrouper par date et heure, puis compter le nombre de transactions. | ||
+ | |||
+ | df.groupby([' | ||
+ | |||
+ | Date | ||
+ | 2016-10-30 09: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | ... ... | ||
+ | 2017-04-09 10: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | [9531 rows x 1 columns] (9531 lignes x 1 colonne) | ||
**So now we have answers for the boss and still all of the work could have been done within the Python Shell. I have created a simple program that contains all of the things that we did in one easy-to-see file. You can find it on pastebin at https:// | **So now we have answers for the boss and still all of the work could have been done within the Python Shell. I have created a simple program that contains all of the things that we did in one easy-to-see file. You can find it on pastebin at https:// | ||
Next month, we’ll continue dealing with Pandas and Python, this time looking at a different dataset. Until then, have fun!** | Next month, we’ll continue dealing with Pandas and Python, this time looking at a different dataset. Until then, have fun!** | ||
+ | |||
+ | Nous avons maintenant les réponses pour notre patron ; et encore une fois, tout ce travail aurait pu être fait dans un shell Python. J'ai créé un programme simple qui contient toutes les choses que nous avons faites dans un fichier facile à visualiser. Vous pouvez le trouver sur pastebin à https:// | ||
+ | |||
+ | Le mois prochain, nous continuerons à traiter de Pandas et Python, en regardant ce coup-là un jeu de données différent. Jusque là, amusez-vous bien ! | ||
issue141/python.1548661612.txt.gz · Dernière modification : 2019/01/28 08:46 de d52fr