real panda
June 6, 2019 * Python Programming

Pandas - Count rows and columns in dataframe

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

References