Exemples de comment obtenir la taille d'un tableau de données DataFrame avec pandas:
Table des matières
Taille d'une DataFrame avec shape()
Soit par exemple le fichier csv suivant train.csv (que l'on peut télécharger sur kaggle). Pour lire le fichier il existe la fonction pandas read_csv():
>>> import pandas as pd>>> df = pd.read_csv('train.csv')
On peut alors utiliser la fonction shape() pour obtenir la taille de la DataFrame:
>>> print(df.shape)(1460, 81)
Nombre de colonnes
Obtenir le nombre de colonnes
>>> df.columnsIndex(['Id', 'MSSubClass', 'MSZoning', 'LotFrontage', 'LotArea', 'Street','Alley', 'LotShape', 'LandContour', 'Utilities', 'LotConfig','LandSlope', 'Neighborhood', 'Condition1', 'Condition2', 'BldgType','HouseStyle', 'OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd','RoofStyle', 'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType','MasVnrArea', 'ExterQual', 'ExterCond', 'Foundation', 'BsmtQual','BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinSF1','BsmtFinType2', 'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'Heating','HeatingQC', 'CentralAir', 'Electrical', '1stFlrSF', '2ndFlrSF','LowQualFinSF', 'GrLivArea', 'BsmtFullBath', 'BsmtHalfBath', 'FullBath','HalfBath', 'BedroomAbvGr', 'KitchenAbvGr', 'KitchenQual','TotRmsAbvGrd', 'Functional', 'Fireplaces', 'FireplaceQu', 'GarageType','GarageYrBlt', 'GarageFinish', 'GarageCars', 'GarageArea', 'GarageQual','GarageCond', 'PavedDrive', 'WoodDeckSF', 'OpenPorchSF','EnclosedPorch', '3SsnPorch', 'ScreenPorch', 'PoolArea', 'PoolQC','Fence', 'MiscFeature', 'MiscVal', 'MoSold', 'YrSold', 'SaleType','SaleCondition', 'SalePrice'],dtype='object')>>> len(df.columns)81
ou en utilisant head()
>>> df.head()Id MSSubClass MSZoning LotFrontage LotArea Street Alley LotShape \0 1 60 RL 65.0 8450 Pave NaN Reg1 2 20 RL 80.0 9600 Pave NaN Reg2 3 60 RL 68.0 11250 Pave NaN IR13 4 70 RL 60.0 9550 Pave NaN IR14 5 60 RL 84.0 14260 Pave NaN IR1LandContour Utilities ... PoolArea PoolQC Fence MiscFeature MiscVal \0 Lvl AllPub ... 0 NaN NaN NaN 01 Lvl AllPub ... 0 NaN NaN NaN 02 Lvl AllPub ... 0 NaN NaN NaN 03 Lvl AllPub ... 0 NaN NaN NaN 04 Lvl AllPub ... 0 NaN NaN NaN 0MoSold YrSold SaleType SaleCondition SalePrice0 2 2008 WD Normal 2085001 5 2007 WD Normal 1815002 9 2008 WD Normal 2235003 2 2006 WD Abnorml 1400004 12 2008 WD Normal 250000[5 rows x 81 columns]
Nombre de lignes
Obtenir le nombre de lignes
>>> df.indexRangeIndex(start=0, stop=1460, step=1)
Références
| Liens | Site |
|---|---|
| pandas.DataFrame.shape | pandas doc |
| head() | pandas doc |
