To retrieve the number of rows from pandas DataFrame using either len()
, axes()
, shape()
and info()
methods. In this article, I will explain how to retrieve the number of rows from pandas DataFrame with examples.
Key Points –
- Use
.shape
attribute to get both the number of rows and columns. .len()
function applied to the DataFrame can return the number of rows.- Access
.shape[0]
to specifically retrieve the number of rows. .index.size
can also be used to return the number of rows in the DataFrame.- The
.info()
method also provides a summary including the row count. - If working with a Numpy array, the number of rows can also be retrieved similarly with
.shape[0]
.
Quick Examples of Retrieve Number Rows From DataFrame
If you are in a hurry, below are some quick examples of how to retrieve number rows from DataFrame.
# Quick examples of retrieve number rows from dataframe
# Using df.axes() method
# To get number rows
rows_count = len(df.axes[0])
# Using DataFrame.len() method
df2 = len(df)
# Return number of rows
# Using df.len()
df2 = len(df.index)
# Using DataFrame.shape method
df2 = df.shape[0]
# Get the number of columns and rows
df2 = df.shape
# Using DataFrame.info() method
df.info()
Now, let’s create a DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names Courses
, Fee
, Duration
, and Discount
.
# Create DataFrame
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Python","pandas","Hadoop"],
'Fee' :[20000,25000,22000,24000,30000],
'Duration':['30days','40days','35days','60days','50days'],
'Discount':[1000,2300,2500,2000,3000]
}
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame(technologies,index=index_labels)
print(df)
Yields below output.
# Output:
Courses Fee Duration Discount
r1 Spark 20000 30days 1000
r2 PySpark 25000 40days 2300
r3 Python 22000 35days 2500
r4 pandas 24000 60days 2000
r5 Hadoop 30000 50days 3000
Using DataFrame.axes() Method to Retrieve Number Rows
Use pandas.DataFrame.axes()
method to retrieve the number of rows (count of rows). It accepts the argument ‘1’ for columns and ‘0’ for rows. For instance, len(df.axes[0])
to returns the number of rows.
# Using df.axes() method to get number rows
rows = len(df.axes[0])
df2 = str(rows)
print("Get number of Rows: " + df2)
Yields below output.
# Output:
Get number of Rows: 5
Yields same output as above.
Using DataFrame.len() Method to Retrieve Number Rows
You can also use DataFrame.len()
method to retrieve a list of the rows in DataFrame. For example, len(df.index)
returns the count of rows.
# Using DataFrame.len() method
df2 = len(df)
print("Get number of rows:", df2)
# Return number of rows using df.len()
df2 = len(df.index)
print("Get number of rows:", df2)
Yields the same output as above.
Retrieve Number Rows to Using DataFrame.shape() Method
Use DataFrame.shape
to get the number of columns and rows as a shape of the DataFrame, which is a tuple where the shape[0]
element is a number of rows and shape[1]
is the number of columns. For example df.shape[0]
gets the number of rows.
# Using DataFrame.shape method
df2 = df.shape[0]
print("Get number of rows:", df2)
# Get the number of columns and rows
df2 = df.shape
print(df2)
Yields the same output as above.
Using DataFrame.info() Method
DataFrame.info()
method provides information about the DataFrame including dtype of columns and index, memory usage, number of columns, etc.
# Using DataFrame.info() method
df.info()
Yields below output.
# Output:
<class 'pandas.core.frame.DataFrame'>
Index: 5 entries, r1 to r5
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Courses 5 non-null object
1 Fee 5 non-null int64
2 Duration 5 non-null object
3 Discount 5 non-null int64
dtypes: int64(2), object(2)
memory usage: 200.0+ bytes
Complete Example For Retrieve Number Rows From DataFrame
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Python","pandas","Hadoop"],
'Fee' :[20000,25000,22000,24000,30000],
'Duration':['30days','40days','35days','60days','50days'],
'Discount':[1000,2300,2500,2000,3000]
}
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame(technologies,index=index_labels)
print(df)
# Using df.axes() method to get number rows
rows = len(df.axes[0])
df2 = str(rows)
print("Get number of Rows: " + df2)
# Using DataFrame.len() method
df2 = len(df)
print("Get number of rows:", df2)
# Return number of rows using df.len()
df2 = len(df.index)
print("Get number of rows:", df2)
# Using DataFrame.shape method
df2 = df.shape[0]
print("Get number of rows:", df2)
# Get the number of columns and rows
df2 = df.shape
print(df2)
# Using DataFrame.info() method
df.info()
Conclusion
In this article, you have learned how to retrieve the number of rows from pandas DataFrame using DataFrame.axes()
, DataFrame.len()
,DataFrame.shape()
and DataFrame.info()
method with examples.
Happy Learning !!
Related Articles
- Replace NaN with Blank/Empty String in Pandas
- Append a List as a Row to Pandas DataFrame
- Pandas Write DataFrame to CSV File
- Pandas Shuffle DataFrame Rows Examples
- Pandas Drop Multiple Columns From DataFrame
- Pandas Get Row Number of DataFrame
- Pandas – Retrieve Number of Rows From DataFrame
- How to Install Anaconda & Run pandas on Jupyter Notebook