• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:8 mins read
You are currently viewing Pandas – Retrieve Number of Rows From DataFrame

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.

1. 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.


# Below are some quick examples
# 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

2. 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.

3. 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 same output as above.

4. 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 same output as above.

5. 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

6. 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 !!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply