Python Data Science Pandas Shape

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]})
task score
0 A 66
1 B 22
2 C 83
3 D 40
4 E 59
5 F 75
(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!

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join our mailing list

Join our mailing list to receive awesome articles about learning Python and Computer Science in a fun and accessible way, straight to your inbox.