issue140:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| issue140:python [2018/12/28 18:08] – créée auntiee | issue140:python [2019/01/08 16:16] (Version actuelle) – auntiee | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | Pandas and Python and Code ... Oh My! | + | **Pandas and Python and Code ... Oh My! |
| I try to keep abreast of new happenings in the Python and programming worlds in general. | I try to keep abreast of new happenings in the Python and programming worlds in general. | ||
| Ligne 10: | Ligne 10: | ||
| • NumPy 1.9.0 or higher | • NumPy 1.9.0 or higher | ||
| • python-dateutil 2.5.0 or higher | • python-dateutil 2.5.0 or higher | ||
| + | • pytz** | ||
| + | |||
| + | Pandas et Python et Code ... Oh mon D... ! | ||
| + | |||
| + | En général, j' | ||
| + | |||
| + | Pandas, pour citer leur propre page Web, « est une bibliothèque Open Source, sous licence BSD, fournissant des outils pour les structures de données et leur analyse, à haute performance et faciles à utiliser, pour le langage de programmation Python. » | ||
| + | |||
| + | Vous pouvez en découvrir beaucoup plus à son sujet sur https:// | ||
| + | • Python 2.7 ou + (au 1er janvier 2019, elle ne marchera que sous Python 3.5 ou +) | ||
| + | • setuptools 24.2.0 ou + | ||
| + | • NumPy 1.9.0 ou + | ||
| + | • python-dateutil 2.5.0 ou + | ||
| • pytz | • pytz | ||
| - | So, given that Pandas is such an important library for Data Science, I plan to spend a few articles on it. Now, I am not going to try to teach you a large amount about Pandas in this quick article. | + | **So, given that Pandas is such an important library for Data Science, I plan to spend a few articles on it. Now, I am not going to try to teach you a large amount about Pandas in this quick article. |
| Pandas can deal with three different types of data structures - | Pandas can deal with three different types of data structures - | ||
| Ligne 19: | Ligne 32: | ||
| • Panel | • Panel | ||
| - | A Series data structure is a 1D labeled array which is size-immutable and contains homogeneous data (same data type). | + | A Series data structure is a 1D labeled array which is size-immutable and contains homogeneous data (same data type). |
| - | All of that dry information is nice, but to really appreciate how easy Pandas makes dealing with data, let's play a little bit. One of the best things about Pandas is that many times, you can do much of the work in a Python shell. | + | Ainsi, étant donné que Pandas est une bibliothèque aussi importante pour la Science des données, je prévois de faire quelques articles dessus. Et je ne vais pas essayer de vous apprendre une part étendue de Pandas dans ce court article. Je vais juste essayer de vous montrer quelques-uns des trucs chouettes que Pandas peut faire. Nous approfondirons Pandas dans les prochains articles. |
| + | |||
| + | Pandas peut gérer trois types de structures de données : | ||
| + | • Series | ||
| + | • DataFrame | ||
| + | • Panel | ||
| + | |||
| + | Une structure de données « Series » est un tableau étiqueté à une dimension de taille non modifiable et qui contient des données homogènes (des données de même type). Une structure « Dataframe » est une structure tabulaire étiquetée à deux dimensions de taille modifiable, qui contient des données hétérogènes (des données de types différents) et qui est un conteneur pour une structure Series. Un « Panel » est un tableau étiqueté en trois dimensions de taille modifiable qui contient des données hétérogènes et qui est un conteneur pour une structure Dataframe. Toutes les structures de données de Pandas sont mutables en valeur (elles peuvent être modifiées). Les modifications de taille ne sont possibles que pour les structures Dataframe et Panel. | ||
| + | |||
| + | **All of that dry information is nice, but to really appreciate how easy Pandas makes dealing with data, let's play a little bit. One of the best things about Pandas is that many times, you can do much of the work in a Python shell. | ||
| So, assuming you've gotten the Pandas library, open up a Python3 shell. | So, assuming you've gotten the Pandas library, open up a Python3 shell. | ||
| Ligne 27: | Ligne 49: | ||
| >>> | >>> | ||
| - | Just for those who haven' | + | Just for those who haven' |
| + | |||
| + | C'est bien, toute cette information brute, mais pour apprécier réellement comment Pandas facilite la gestion des données, jouons un peu avec. Une des meilleures choses à propos de Pandas est que, souvent, vous pouvez faire la plupart de votre travail dans un shell Python. | ||
| + | |||
| + | Aussi, en présumant que vous avez récupéré la bibliothèque Pandas, ouvrons un shell Python3. La première chose que vous devez faire est d' | ||
| + | |||
| + | >>> | ||
| + | |||
| + | Juste pour ceux qui n' | ||
| - | Series Data Structures | + | **Series Data Structures |
| Now let's create a simple list of random ten integers and name it ' | Now let's create a simple list of random ten integers and name it ' | ||
| Ligne 40: | Ligne 70: | ||
| That's all there is to it. Now let's see what it looks like.. | That's all there is to it. Now let's see what it looks like.. | ||
| + | |||
| + | >>> | ||
| + | 0 20 | ||
| + | 1 10 | ||
| + | 2 42 | ||
| + | 3 73 | ||
| + | 4 90 | ||
| + | 5 18 | ||
| + | 6 37 | ||
| + | 7 26 | ||
| + | 8 19 | ||
| + | 9 98** | ||
| + | |||
| + | Structure de données Series | ||
| + | |||
| + | Maintenant, créons une liste simple de dix entiers aléatoires et appelons-la « data ». | ||
| + | |||
| + | >>> | ||
| + | |||
| + | Ensuite, nous pouvons créer une structure de données Series de Pandas avec la commande .Series(). | ||
| + | |||
| + | >>> | ||
| + | |||
| + | C'est tout ce qu'il y a à faire. Maintenant, regardons à quoi ça ressemble. | ||
| >>> | >>> | ||
| Ligne 53: | Ligne 107: | ||
| 9 98 | 9 98 | ||
| - | Notice our list of integers is exactly as we entered it and that there is an index added for us as well. This is the DEFAULT index. | + | **Notice our list of integers is exactly as we entered it and that there is an index added for us as well. This is the DEFAULT index. |
| Also, you might notice that at the end of the output from almost all of the Pandas code we will be doing, you will see something like dtype: int64. | Also, you might notice that at the end of the output from almost all of the Pandas code we will be doing, you will see something like dtype: int64. | ||
| Now, if we just want a quick peek at the data, we could use the .head() or .tail() command. | Now, if we just want a quick peek at the data, we could use the .head() or .tail() command. | ||
| + | |||
| + | >>> | ||
| + | 0 20 | ||
| + | 1 10 | ||
| + | 2 42 | ||
| + | 3 73 | ||
| + | 4 90** | ||
| + | |||
| + | Notez que notre liste d' | ||
| + | |||
| + | Vous pouvez noter aussi qu'à la fin des sorties de presque tous les codes de Pandas que nous ferons, vous verrez quelque chose comme dtype: int64. C'est là pour vous montrer quel est le type des données. Je l'ai retiré des listings imprimés dans cet article pour gagner de la place. | ||
| + | |||
| + | Maintenant, si vous voulez simplement un extrait rapide des données, nous pouvons utiliser la commande .head() ou .tail(). Voici un exemple de la commande .head() montrant les cinq premiers éléments. | ||
| >>> | >>> | ||
| Ligne 66: | Ligne 133: | ||
| 4 90 | 4 90 | ||
| - | The .tail() command works the same way, showing the end of the data list. | + | **The .tail() command works the same way, showing the end of the data list. |
| If we want to simply see one item out of our list, we can use the index... | If we want to simply see one item out of our list, we can use the index... | ||
| - | |||
| >>> | >>> | ||
| Ligne 75: | Ligne 141: | ||
| Now assume we want to see items 4, 5 and 6. We do it this way... | Now assume we want to see items 4, 5 and 6. We do it this way... | ||
| + | |||
| + | >>> | ||
| + | 4 90 | ||
| + | 5 18 | ||
| + | 6 37** | ||
| + | |||
| + | La commande .tail() fonctionne de la même façon, en montrant la fin de la liste. | ||
| + | |||
| + | Si vous ne voulez voir qu'un des éléments de la liste, vous pouvez utiliser l' | ||
| + | |||
| + | >>> | ||
| + | 90 | ||
| + | |||
| + | Maintenant, disons que nous voulons voir les éléments 4,5 et 6. Nous le faisons ainsi... | ||
| >>> | >>> | ||
| Ligne 81: | Ligne 161: | ||
| 6 37 | 6 37 | ||
| - | While this command looks somewhat strange, I'll break it down so it makes more sense... | + | **While this command looks somewhat strange, I'll break it down so it makes more sense... |
| sd.loc[] is an indexer command. | sd.loc[] is an indexer command. | ||
| Ligne 90: | Ligne 170: | ||
| Now getting back to custom indexes. | Now getting back to custom indexes. | ||
| + | |||
| + | >>> | ||
| + | |||
| + | >>> | ||
| + | One 20 | ||
| + | Two 10 | ||
| + | Three 42 | ||
| + | Four 73 | ||
| + | Five 90 | ||
| + | Six 18 | ||
| + | Seven 37 | ||
| + | Eight 26 | ||
| + | Nine 19 | ||
| + | Ten 98** | ||
| + | |||
| + | Comme cette commande paraît un peu étrange, je la détaillerai pour lui donner plus du sens... | ||
| + | |||
| + | sd.loc[] est une commande d' | ||
| + | |||
| + | .loc[rowslice, | ||
| + | |||
| + | Comme nous utilisons une structure Series, nous n' | ||
| + | |||
| + | Maintenant, revenons aux index personnalisés. Nous pouvons créer l' | ||
| >>> | >>> | ||
| Ligne 105: | Ligne 209: | ||
| Ten 98 | Ten 98 | ||
| - | One of the things that I really like about Pandas, is the built-in Data Analysis Helper functions. | + | **One of the things that I really like about Pandas, is the built-in Data Analysis Helper functions. |
| + | |||
| + | >>> | ||
| + | 433 | ||
| + | >>> | ||
| + | 10 | ||
| + | >>> | ||
| + | 10 | ||
| + | >>> | ||
| + | 98 | ||
| + | >>> | ||
| + | count 10.000000 | ||
| + | mean | ||
| + | std 32.107631 | ||
| + | min 10.000000 | ||
| + | 25% 19.250000 | ||
| + | 50% 31.500000 | ||
| + | 75% 65.250000 | ||
| + | max 98.000000** | ||
| + | |||
| + | Une des choses que j' | ||
| >>> | >>> | ||
| Ligne 125: | Ligne 249: | ||
| max 98.000000 | max 98.000000 | ||
| - | DataFrame Data Structures | + | **DataFrame Data Structures |
| Now that I’ve shown you some of the things that can be done with a simple Series data structure, let’s look at the DataFrame. | Now that I’ve shown you some of the things that can be done with a simple Series data structure, let’s look at the DataFrame. | ||
| Ligne 134: | Ligne 258: | ||
| • Other DataFrames | • Other DataFrames | ||
| - | The easiest way to show you a DataFrame in action, let’s create a small dictionary (shown below). | + | The easiest way to show you a DataFrame in action, let’s create a small dictionary (shown below).** |
| - | As you can see, there will be four rows and four columns. | + | Structure de données Dataframe |
| + | |||
| + | Maintenant que je vous ai montré certaines des choses qui peuvent être faites avec une simple structure de données Series, regardons les Dataframes. J'ai indiqué précédemment qu'une Dataframe était une structure tabulaire en deux dimensions. Pensez à un tableur ou à une table de base de données et c'est à peu près à quoi ressemble une Dataframe. Nous pouvons créer une Dataframe à partir de n' | ||
| + | • Listes | ||
| + | • Dictionnaires | ||
| + | • Séries | ||
| + | • Numpy ndarrays | ||
| + | • Autres DataFrames | ||
| + | |||
| + | La façon la plus simple de vous montrer une Dataframe en action sera de créer un petit dictionnaire (montré ci-dessous). | ||
| + | |||
| + | **As you can see, there will be four rows and four columns. | ||
| >>> | >>> | ||
| Ligne 148: | Ligne 283: | ||
| 2 Mary | 2 Mary | ||
| Human Resources | Human Resources | ||
| + | 3 Lois | ||
| + | >>> | ||
| + | |||
| + | Comme vous pouvez le voir, il y a quatre lignes et quatre colonnes. Et vous pouvez voir aussi que les types de données sont variés. Comme nous l' | ||
| + | |||
| + | >>> | ||
| + | |||
| + | Maintenant, pour voir à quoi ressemble la structure à Pandas, nous appelons simplement la structure. | ||
| + | |||
| + | >>> | ||
| + | | ||
| + | 0 Greg | ||
| + | 1 | ||
| + | 2 Mary | ||
| 3 Lois | 3 Lois | ||
| >>> | >>> | ||
| - | Like I said earlier, it resembles a spreadsheet. | + | **Like I said earlier, it resembles a spreadsheet. |
| >>> | >>> | ||
| Ligne 161: | Ligne 310: | ||
| Now that we have our age Serial structure, let’s get the sum of the values… | Now that we have our age Serial structure, let’s get the sum of the values… | ||
| + | |||
| + | >>> | ||
| + | 167** | ||
| + | |||
| + | Comme je l'ai déjà dit, cela ressemble à un tableur. À peu près tout ce que nous avons fait avec la structure Series peut être fait avec la Dataframe. Faisons quelque chose d' | ||
| + | |||
| + | >>> | ||
| + | >>> | ||
| + | 0 65 | ||
| + | 1 34 | ||
| + | 2 41 | ||
| + | 3 27 | ||
| + | |||
| + | Maintenant que nous avons notre structure Series age, additionnons les valeurs... | ||
| >>> | >>> | ||
| 167 | 167 | ||
| - | It’s SO easy to deal with data this way. | + | **It’s SO easy to deal with data this way. |
| As we did with the Serial structure, we can get the data for just one row by using the .loc command. | As we did with the Serial structure, we can get the data for just one row by using the .loc command. | ||
| Ligne 178: | Ligne 341: | ||
| Notice we have to use the index that was created for us automatically. | Notice we have to use the index that was created for us automatically. | ||
| - | >>> | + | >>> |
| + | |||
| + | C'est TELLEMENT facile de gérer les données de cette façon. | ||
| + | |||
| + | Comme nous l' | ||
| + | |||
| + | >>> | ||
| + | Nom Greg | ||
| + | Age 65 | ||
| + | Sexe M | ||
| + | Service | ||
| + | Name: 0, dtype: object | ||
| + | |||
| + | Notez que nous avons utilisé l' | ||
| + | |||
| + | >>> | ||
| - | Now we can see our data structure after the change... | + | **Now we can see our data structure after the change... |
| >>> | >>> | ||
| Ligne 197: | Ligne 375: | ||
| Gender | Gender | ||
| Department | Department | ||
| + | Name: Greg, dtype: object** | ||
| + | |||
| + | Maintenant, vous pouvez voir notre structure de données après la modification... | ||
| + | |||
| + | >>> | ||
| + | Nom Age Sexe | ||
| + | Greg | ||
| + | Sam 34 M Development | ||
| + | Mary | ||
| + | Lois | ||
| + | |||
| + | Maintenant, notre index est remplacé par la colonne Nom. MAINTENANT, nous pouvons obtenir uniquement l' | ||
| + | |||
| + | >>> | ||
| + | Age 65 | ||
| + | Sexe M | ||
| + | Service | ||
| Name: Greg, dtype: object | Name: Greg, dtype: object | ||
| - | One of the things we can do with a DataFrame that we can’t with a Serial structure, is get extended information using the .info() command. | + | **One of the things we can do with a DataFrame that we can’t with a Serial structure, is get extended information using the .info() command. |
| >>> | >>> | ||
| Ligne 212: | Ligne 407: | ||
| memory usage: 208.0+ bytes | memory usage: 208.0+ bytes | ||
| - | I hope that I have generated some interest about Pandas. | + | I hope that I have generated some interest about Pandas. |
| + | |||
| + | Une des choses que nous pouvons faire avec une Dataframe, que nous ne pouvons pas faire avec une structure Series, c'est d' | ||
| + | |||
| + | >>> | ||
| + | <class ' | ||
| + | RangeIndex: 4 entries, 0 to 3 | ||
| + | Data columns (total 4 columns): | ||
| + | Nom 4 non-null object | ||
| + | Age 4 non-null int64 | ||
| + | Sexe 4 non-null object | ||
| + | Service | ||
| + | dtypes: int64(1), object(3) | ||
| + | memory usage: 208.0+ bytes | ||
| + | |||
| + | J' | ||
issue140/python.1546016887.txt.gz · Dernière modification : 2018/12/28 18:08 de auntiee
