Let's break down the ways to get the number of rows in a Pandas DataFrame with detailed explanations and examples.
1. Using the len()
Function
The len()
function in Python returns the number of items in an object. When you apply len()
to a Pandas DataFrame, it returns the number of rows in the DataFrame.
Example:
import pandas as pd
data = {
"name": ["John", "Jane", "Jade"],
"age": [2, 10, 3]
}
df = pd.DataFrame(data)
num_of_rows = len(df)
print(f"The number of rows is {num_of_rows}")
Explanation:
- We import the Pandas library.
- Create a dictionary named
data
with some data. - Convert this dictionary into a DataFrame
df
. - Use
len(df)
to get the number of rows indf
. - Print the result.
Output:
The number of rows is 3
2. Using the shape
Attribute
The shape
attribute of a DataFrame returns a tuple representing the dimensions of the DataFrame. The first element of the tuple is the number of rows, and the second is the number of columns.
Example:
num_of_rows = df.shape[0]
print(f"The number of rows is {num_of_rows}")
Explanation:
-
df.shape
returns a tuple, where the first element ([0]
) is the number of rows. - Print the number of rows.
Output:
The number of rows is 3
3. Using the index
Attribute
The index
attribute provides access to the index (row labels) of the DataFrame. You can use the size
property of the index or pass the index to the len()
function to get the number of rows.
Example using index.size
:
num_of_rows = df.index.size
print(f"The number of rows is {num_of_rows}")
Example using len(df.index)
:
num_of_rows = len(df.index)
print(f"The number of rows is {num_of_rows}")
Explanation:
-
df.index.size
andlen(df.index)
both give the number of rows indf
. - Print the number of rows.
Output:
The number of rows is 3
4. Using the axes
Attribute
The axes
attribute returns a list representing the axes of the DataFrame. The first element of the list is an index representing the rows. Similar to using the index
attribute, you can get the size or use len()
on the first element of axes
.
Example using axes[0].size
:
num_of_rows = df.axes[0].size
print(f"The number of rows is {num_of_rows}")
Example using len(df.axes[0])
:
num_of_rows = len(df.axes[0])
print(f"The number of rows is {num_of_rows}")
Explanation:
- Both
df.axes[0].size
andlen(df.axes[0])
return the number of rows indf
. - Print the number of rows.
Output:
The number of rows is 3
Additional: Counting Non-NaN Values in a Column
In cases where you might be interested in the count of non-missing (non-NaN) values in a specific column, you can use the .count()
method on a DataFrame column.
Example:
non_nan_rows = df['age'].count()
print(f"The number of non-NaN rows in the 'age' column is {non_nan_rows}")
Explanation:
-
df['age'].count()
counts the number of non-NaN entries in the 'age' column. - Print the result.
Output:
The number of non-NaN rows in the 'age' column is 3
These examples cover different methods to get the number of rows in a Pandas DataFrame, offering flexibility depending on your specific needs or preferences.
Top comments (0)