Pandas Number Of Rows β 6 Methods
Count rows in pandas DataFrame using six methods
Often times youβll need to know how many rows are in your dataset. Simply, it is the most foundational metric you can know about your data. Plus, you may want to find out how long your .apply() function is going run. Weβll show your 6 pandas ways to count number of rows.
Pandas number of rows will tell youβ¦drumrollβ¦how many rows you have in your dataset. This is important to know before applying an expensive (long running) function to your dataset. It is crucial to understand while getting to know your data.
Lets go over 6 methods in order of our favorites
- DataFrame Length β len(df)
- DataFrame Info β df.info
- DataFrame__len__ β df.__len__
- DataFrame Shape β df.shape
- DF Count β df.count
- DataFrame Axis Length β (df.axes[0])
Pseudo code: Return the number of rows in a pandas DataFrame or Series
Pandas Number Of Rows
6 Methods To Find Row Count
Below are 6 methods to find out how tall your your dataset is. Weβve listed them in order of our favorite to least favorite.
DataFrame Length
len(df)
First up is DataFrame Length. This super easy and fast function will return the length of your DataFrame. The default length is the number of rows in your dataset. This is my #1 go to function to find out row count. len()
come from vanilla python.
DataFrame Info
df.info()
Next is DataFrame Info. Though it is a bit slower, youβll get more information for free. df.info() will return column names, row count, and how many non-na values you have in each row. It is useful when trying to get to know your data. I use this when I want to know row count and the characteristics of my columns.
DataFrame __len__
df.__len__
Fun fact, functions that start with double underscores have a short name of βdunder.β df.__len__ is a pass-through function that simply calls len(df.index)
. It is quick and easy. I donβt use it that often because 1) I have to type out extra characters and 2) the double underscores donβt look clean. But itβs fast!
DataFrame Shape
`df.shape[0] - To count rows
df.shape[1] - To count columns`
With DataFrame shape youβll get the shape
of your DataFrame. Yes I know that sentence is palindrome. Think of shape as the height and width of your table. Youβll be returned a tuple with two values, height and width. Shape works well, but in order to get the row count you need to reference the first item of your tuple via β[0].β
DataFrame Count
df.count()
DataFrame Count will return the number of Non-NA values within each column. I donβt love this one because 1) itβs slower and 2) you need to do extra data work after your call .count(). Be careful, if you have NAs in your dataset, you may get confusing examples. .count() will skip these by default.
DataFrame Axes Length
len(df.axes[0])
Next up is our most verbose option β DataFrame Axes Length. Letβs break this one down. df.axes
will return a tuple of your two axes for rows and columns. [0]
will pull the first item (rows) from your tuple. Then finally len()
will find the length, or how many items, you have in your axis which is your row count.
Letβs look at an examples