Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

introduction


copier une dataframe ou une série

Loading...
Loading...

créer une nouvelle colonne

Loading...

rappels python, numpy

['Hello !', 20, 30, 40, 60]
['Hello !', 200, 300, 500, 40, 60]
array([[999, 999, 2], [999, 999, 5], [ 6, 7, 8], [ 9, 10, 11]])
np.float64(27.0)

localiser en pandas

ligne,colonne vs colonne, ligne


localisation avec loc et iloc

Loading...
Loading...
'Sharp, Mr. Percival James R'
'Sharp, Mr. Percival James R'
'Richards, Master. George Sibley'

sélection multiple

Survived 0 Pclass 2 Name Sharp, Mr. Percival James R Sex male Age 27.0 SibSp 0 Parch 0 Ticket 244358 Fare 26.0 Cabin NaN Embarked S Name: 552, dtype: object
Loading...
Loading...
Loading...
Loading...
Loading...

slicing pandas et bornes


slicing avec loc par index

Index([552, 638, 499, 261, 395], dtype='int64', name='PassengerId')
Index(['Survived', 'Pclass', 'Name', 'Sex', 'Age'], dtype='str')
(3, 4)
Loading...
(1, 3)

slicing avec iloc par indices

(6, 3)

localiser une ligne ou une colonne


exercice sélections multiples et slicing

  1. lisez le titanic et mettez les PassengerId comme index des lignes

  1. localisez l’élément d’index 40
    a. Quel est le type de l’élément ?
    b. localisez le nom du passager d’index 40 ?

  1. quel est le nom de la personne qui apparaît en avant-dernier dans le fichier

  1. localisez les 3 derniers éléments (colonnes) de la ligne d’index 40

  1. localisez les 4 derniers éléments de la colonne Cabin

  1. fabriquez une dataframe contenant

indexation par un masque

rappel sur les masques

taux survie global = 0.38
rate in the group 0.97

sélection par masque booléen

Loading...

df[mask] décortiqué

<class 'pandas.Series'>
bool
(891,)
PassengerId 552 False 638 False 499 True 261 False 395 True Name: Sex, dtype: bool
Loading...
Loading...

exercice combinaison d’expressions booléennes

  1. en une seule ligne sélectionner la sous-dataframe des passagers
    qui ne sont pas en première classe
    et dont l’age est supérieur ou égal à 70 ans

  1. Combien trouvez-vous de passagers ?

  1. Accédez à la valeur Name du premier de ces passagers

  1. Faites la même expression que la question 1
    en utilisant les fonctions numpy.logical_and, numpy.logical_not


résumé des méthodes d’indexation

Loading...
Loading...

règles des modifications

sélections de parties de dataframe


modification d’une copie

np.int64(0)
/tmp/ipykernel_1776/3582046640.py:1: ChainedAssignmentError: A value is being set on a copy of a DataFrame or Series through chained assignment.
Such chained assignment never works to update the original DataFrame or Series, because the intermediate object on which we are setting values always behaves as a copy (due to Copy-on-Write).

Try using '.loc[row_indexer, col_indexer] = value' instead, to perform the assignment in a single step.

See the documentation for a more detailed explanation: https://pandas.pydata.org/pandas-docs/stable/user_guide/copy_on_write.html#chained-assignment
  df['Survived'][552] = 1
np.int64(0)
np.int64(1)
np.int64(0)
nan
/tmp/ipykernel_1776/2655944779.py:8: ChainedAssignmentError: A value is being set on a copy of a DataFrame or Series through chained assignment.
Such chained assignment never works to update the original DataFrame or Series, because the intermediate object on which we are setting values always behaves as a copy (due to Copy-on-Write).

Try using '.loc[row_indexer, col_indexer] = value' instead, to perform the assignment in a single step.

See the documentation for a more detailed explanation: https://pandas.pydata.org/pandas-docs/stable/user_guide/copy_on_write.html#chained-assignment
  df['Age'][889] = 27.5

faire des copies explicites

0
0
0