Need to know the total number of records or rows of data within a dataframe?
The most efficient way is to pull up the index and compute its length. Use code below to count records for 'data_deposits.csv'.
Count rows within dataframe
import pandas as pd df = pd.read_csv( 'data_deposits.csv', header = 0, sep = ',' ) #count rows row_count = len(df.index) print( 'Rows = ', row_count)
Output: Rows = 8
The file 'data_deposits.csv' contains 9 rows, the first one being the header. So when the data is loaded, then the count for valid data rows is eight.
Another possibility is to look up the shape of the dataframe, which provides a (rows, columns) tuple.
Rows and columns from shape
import pandas as pd df = pd.read_csv( 'data_deposits.csv', header = 0, sep = ',' ) #count rows and columns row_count, col_count = df.shape print('Rows:{}'.format(row_count)) print('Cols:{}'.format(col_count))
Output of code: Rows: 8 Cols: 5
There you are... two birds with one stone! We now have the row and column count for a dataframe.
What if we needed the total number of cells, or elements in the dataframe. That would be number of rows times the number of columns per row. It is possible to do the product from the shape values, or just query it using the size function.
Size of a dataframe
import pandas as pd df = pd.read_csv( 'data_deposits.csv', header = 0, sep = ',' ) #count cell elements cells = df.size print("Cells: ",cells)
Output of code: Cells: 40
# Python - Delete multiple elements from a list
# SEO Google page rank and web traffic
# Python: Random access generator for multi value sublist yield
# Python: Enumerate counter for loops over list, tuple, string
# Pandas - Read, skip and customize column headers for read_csv
# Pandas - Selecting data rows and columns using read_csv
# Pandas - Space, tab and custom data separators
# Sample data for Python tutorials
# Pandas - Purge duplicate rows
# Pandas - Concatenate or vertically merge dataframes
# Pandas - Search and replace values in columns
# Pandas - Count rows and columns in dataframe
# Pandas - Adding new static columns
# Python - Hardware and operating system information
# Pandas - Remove or drop columns from Pandas dataframe