Examples of how to get the shape of a DataFrame in python with panda:
Table of contents
Get DataFrame shape
Let's consider the following file train.csv (that can be downloaded on kaggle):
>>> import pandas as pd>>> df = pd.read_csv('train.csv')
To get the shape a solution is to use the function shape():
>>> print(df.shape)(1460, 81)
Number of columns
Get the number of columns
>>> 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
Another solution using using 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]
Number of lines
Get the number of lines
>>> df.indexRangeIndex(start=0, stop=1460, step=1)
References
| Links | Site |
|---|---|
| pandas.DataFrame.shape | pandas doc |
| head() | pandas doc |
