Tuesday, June 23, 2020

Python pandas get length (row count) of a dataframe

There are several ways of knowing the length of your dataframe rows. Here are three ways using index, shape and count methods.

Manually inspecting this dataframe below, we see that it has 10 rows (note the index count starts from 0). Now, let check for this information dynamically.



1) index
Using the index method, it returns the 'RangeIndex' start, stop and step for the dataframe. So, wrapping it in a len() function will give the stop value that is the length of the datafarme as seen below.





2) shape
Using the shape method will return a tuple like so (row, col), where the first element is row and the second element is column.




3) count
Using the count method returns a 'pandas.core.series.Series' that contains number of items for each column.



Enjoy!

No comments:

Post a Comment