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:44] – d52fr | issue141:python [2019/02/08 16:06] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Pandas = Cuddly... Data? | + | **Pandas = Cuddly... Data? |
This time, we will concentrate on the Pandas DataFrame and dealing with a semi-real world scenario. | This time, we will concentrate on the Pandas DataFrame and dealing with a semi-real world scenario. | ||
Ligne 5: | Ligne 5: | ||
You'll need to download a CSV file from kaggle.com. The link is https:// | You'll need to download a CSV file from kaggle.com. The link is 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:// | + | 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:// |
- | The data that the CSV file holds is really rather simple. There are just four columns... | + | 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... | ||
• Date | • Date | ||
• Time | • Time | ||
Ligne 25: | Ligne 33: | ||
2016-10-30, | 2016-10-30, | ||
- | Of course, this is just the first 8 lines from the file. | + | Of course, this is just the first 8 lines from the 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. | + | 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. | ||
import pandas as pd | import pandas as pd | ||
Ligne 41: | Ligne 70: | ||
All of the data is really there, but Pandas only shows a portion of the DataFrame information. | All of the data is really there, but Pandas only shows a portion of the DataFrame information. | ||
- | 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 ' |
- | # get and display a list of the column names (headers) | + | 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) | ||
col_list = df.columns.values.tolist() | col_list = df.columns.values.tolist() | ||
Ligne 54: | Ligne 99: | ||
We can also simply call df.count() and it will show us something like this... | We can also simply call df.count() and it will show us something like this... | ||
+ | |||
+ | print(df.count()) | ||
+ | |||
+ | Date 21293 | ||
+ | Time 21293 | ||
+ | Transaction | ||
+ | Item 21293 | ||
+ | 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()) | print(df.count()) | ||
Ligne 63: | Ligne 128: | ||
dtype: int64 | 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… |
datelist = df[' | datelist = df[' | ||
Ligne 74: | Ligne 139: | ||
159 2016-10-30 2017-04-09 | 159 2016-10-30 2017-04-09 | ||
- | Keep the datelist variable in mind for later on. | + | Keep the datelist variable in mind for later on.** |
- | We also know that we have the ' | + | 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 ' | ||
itemlist = df[' | itemlist = df[' | ||
Ligne 88: | Ligne 168: | ||
[' | [' | ||
- | 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.** |
- | 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... | + | 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... | ||
• By day, how many of each unique item was sold? | • By day, how many of each unique item was sold? | ||
• By item, what were the top 10 sellers? What were the bottom 10? | • By item, what were the top 10 sellers? What were the bottom 10? | ||
Ligne 99: | Ligne 193: | ||
By day, how many of each unique item was sold? | By day, how many of each unique item was sold? | ||
- | 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. |
Sale #2 (transaction 1955) was completed on the same day at 10:23:10, and was for two items. | Sale #2 (transaction 1955) was completed on the same day at 10:23:10, and was for two items. | ||
So how would we structure our research to accomplish the task? If I were to simply look at a single day, I would get all of the records for the day in question and sort the records by the Items sold. I would then count each unique item that was sold for that day. Using the five record set above, it would look something like this... | So how would we structure our research to accomplish the task? If I were to simply look at a single day, I would get all of the records for the day in question and sort the records by the Items sold. I would then count each unique item that was sold for that day. Using the five record set above, it would look something like this... | ||
+ | |||
+ | Date | ||
+ | -----------|-----------|----- | ||
+ | 2016-11-24 | Bread | 2 | ||
+ | | Coffee | ||
+ | | 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 | Date | ||
Ligne 113: | Ligne 230: | ||
| Alfajores | 1 | | 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. |
So, how would we get from the output of a simple set of records to a command set that gets us what we want for the full data set? The key is in the phrases ' | So, how would we get from the output of a simple set of records to a command set that gets us what we want for the full data set? The key is in the phrases ' | ||
Ligne 125: | Ligne 242: | ||
So now, we know how to get the data for the boss for question #1. How about question #2... | So now, we know how to get the data for the boss for question #1. How about question #2... | ||
- | #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?** |
- | 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. | + | 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. | ||
+ | |||
+ | # 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)) | ||
+ | |||
+ | #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? | # By item, what were the top 10 sellers? What were the bottom 10? | ||
Ligne 139: | Ligne 284: | ||
print(sorteditemcount2.tail(10)) | print(sorteditemcount2.tail(10)) | ||
- | #3 - By day, what were the busiest times? | + | 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. |
df.groupby([' | df.groupby([' | ||
Ligne 162: | Ligne 307: | ||
| | ||
- | [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:// | ||
+ | |||
+ | Next month, we’ll continue dealing with Pandas and Python, this time looking at a different dataset. Until then, have fun!** | ||
- | So now we have answers for the boss and still all of the work could have been done within the Python | + | Nous avons maintenant les réponses pour notre patron ; et encore une fois, tout ce travail aurait pu être fait dans un shell Python. |
- | Next month, we’ll continue dealing with Pandas | + | Le mois prochain, nous continuerons à traiter de Pandas |
issue141/python.1548661456.txt.gz · Dernière modification : 2019/01/28 08:44 de d52fr