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/29 13:51] – d52fr | issue141:python [2019/02/08 16:06] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 7: | Ligne 7: | ||
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 = Tendres | + | Pandas = câlins |
- | Cette fois, nous nous concentrerons sur les Dataframes | + | Cette fois, nous nous concentrerons sur les DataFrames |
Nous aurons besoin de télécharger un fichier CSV depuis kaggle.com. Le lien est https:// | Nous aurons besoin de télécharger un fichier CSV depuis kaggle.com. Le lien est https:// | ||
- | Une fois que vous l'avez téléchargé, | + | Une fois que vous l'aurez téléchargé, |
**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 40: | Ligne 40: | ||
• Transaction (un nombre) | • Transaction (un nombre) | ||
• Item (l' | • Item (l' | ||
- | et 21,293 lignes. | + | et 21 293 lignes. |
- | Pour commencer, nous créerons une Dataframe | + | Pour commencer, nous créerons une DataFrame |
Date, | Date, | ||
Ligne 72: | Ligne 72: | ||
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' | + | Pour commencer, nous importerons Pandas (exactement comme nous l' |
import pandas as pd | import pandas as pd | ||
Ligne 84: | Ligne 84: | ||
Ce que nous verrons ressemblera aux données montrées ci-dessus. | 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 information | + | Toutes les données sont bien là, mais Pandas ne montre qu'une partie des informations |
- | Maintenant, | + | Maintenant, |
**# get and display a list of the column names (headers) | **# get and display a list of the column names (headers) | ||
Ligne 118: | Ligne 118: | ||
[' | [' | ||
- | nous pouvons | + | nous pouvons |
print(df.count()) | print(df.count()) | ||
Ligne 141: | Ligne 141: | ||
Keep the datelist variable in mind for later on.** | Keep the datelist variable in mind for later on.** | ||
- | Donc, jusqu'ici, nous avons créé et chargé notre Dataframe | + | Jusqu'ici, nous avons créé et chargé notre DataFrame |
datelist = df[' | 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. | + | 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), | print(len(datelist), | ||
- | dont les résultats sont… | + | dont les résultats sont : |
159 2016-10-30 2017-04-09 | 159 2016-10-30 2017-04-09 | ||
Ligne 170: | Ligne 170: | ||
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 qu' | + | Nous savons aussi que la colonne « Item » existe. Nous pouvons faire la même chose pour voir combien d' |
itemlist = df[' | itemlist = df[' | ||
Ligne 182: | Ligne 182: | ||
[' | [' | ||
- | Bon. Maintenant nous savons que nous avons une Dataframe | + | Bon. Maintenant nous savons que nous avons une DataFrame |
**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 195: | Ligne 195: | ||
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' | + | Avant d' |
• Par jour, combien d' | • Par jour, combien d' | ||
• Par article, quels ont été les plus vendus ? Quels sont les 10 moins bien vendus ? | • Par article, quels ont été les plus vendus ? Quels sont les 10 moins bien vendus ? | ||
- | • Par jour, quels sont ceux les plus actifs | + | • Par jour, quels sont les périodes |
- | Avant de pouvoir répondre à ces questions, nous devons monter un plan pour chacune. Aussi, commençons avec la question n°1... | + | 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' | Par jour, combien d' | ||
Ligne 222: | Ligne 222: | ||
La vente n°2 (transaction 1955) s'est terminée le même jour à 10:23:10, pour deux articles. | 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. | + | 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. |
Date | Date | ||
Ligne 243: | 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 257: | 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 280: | 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.1548766296.txt.gz · Dernière modification : 2019/01/29 13:51 de d52fr