Page 52 - Informatics_Practices_Fliipbook_Class12
P. 52
To know the number of rows or the number of columns in a DataFrame, we use the len() function:
>>> nRows = len(groceryDF.index)
>>> nCols = len(groceryDF.columns)
>>> print("Number of rows:", nRows)
Number of rows: 8
>>> print("Number of columns:", nCols)
Number of columns: 4
index attribute yields row labels.
columns attribute yields column attributes.
len() function yields the number of rows/columns.
Alternatively, we can use the size attribute of the DataFrame to retrieve the number of rows or the number of
columns in a DataFrame, as shown below:
>>> nRows = groceryDF.index.size
>>> nCols = groceryDF.columns.size
>>> print("Number of rows:", nRows)
Number of rows: 8
>>> print("Number of columns:", nCols)
Number of columns: 4
2.5 Summary Information about a DataFrame
The info() method of a DataFrame provides the following summary information about a DataFrame:
• The number of rows, and the row indexes.
• The names of columns, the number of non-null entries in each column, and the type objects in each column, as
shown below:
>>> groceryDF.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 8 entries, 0 to 7
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Product 8 non-null object
1 Category 8 non-null object
2 Price 8 non-null int64
3 Quantity 8 non-null int64
dtypes: int64(2), object(2)
memory usage: 384.0+ bytes
Note that the above DataFrame has 8 entries, which is the same as the number of rows. The total of 4 columns is given
in the "Data columns" section.
Attribute ndim of Pandas DataFrame: Yields the number of dimensions of a DataFrame.
Attribute shape of the DataFrame: Returns a tuple of the form (n_rows, n_cols).
Attributes index: Labels along the rows (row labels).
Attributes columns: Labels used across columns (column names) of the dataframe.
info() method of a DataFrame: Returns the number of rows, and the row indexes, the names of columns, the
number of non-null entries in each column, and the type objects in each column.
38 Touchpad Informatics Practices-XII

