Finding the Size of a Pandas Dataframe

Here’s a useful tip for finding the size of a pandas dataframe. It can get a little confusing as len and size usually give different answers.

size gives the total number of values, while len give the length of the dataframe. So, for example, if you have 3 rows and 2 columns, size will be 6, while len will be 3.

A great way to avoid this potential confusion is to use shape instead of either len or size. This explicitly gives you the number of rows and the number of columns.

You can see this illustrated in the code below:

import pandas as pd

df = pd.DataFrame({'task':['A','B','C','D','E','F'],'score':[66, 22, 83, 40, 59, 75]})
df
task score
0 A 66
1 B 22
2 C 83
3 D 40
4 E 59
5 F 75
df.size
12
len(df)
6
df.shape
(6, 2)

This post has shown how to find the dimensions of a pandas dataframe by using shape, and avoid confusion that can arise from using len or size.

Happy computing!

Sharing is caring!

Leave a Reply

Your email address will not be published.