How do I get the row count of a Pandas DataFrame?

 

To get the row count of a Pandas DataFrame, you can use the shape attribute or the len() function. Here are examples of both methods:

Using the shape attribute:

python
import pandas as pd # Create a sample DataFrame data = {'Column1': [1, 2, 3, 4, 5], 'Column2': ['A', 'B', 'C', 'D', 'E']} df = pd.DataFrame(data) # Get the row count using the shape attribute row_count = df.shape[0] print("Row count:", row_count)

In this example, the shape attribute of the DataFrame df returns a tuple where the first element is the number of rows, so df.shape[0] gives you the row count.

Using the len() function:

python
import pandas as pd # Create a sample DataFrame data = {'Column1': [1, 2, 3, 4, 5], 'Column2': ['A', 'B', 'C', 'D', 'E']} df = pd.DataFrame(data) # Get the row count using the len() function row_count = len(df) print("Row count:", row_count)

In this example, you can directly use the len() function on the DataFrame df to obtain the row count.

Both methods will give you the number of rows in the DataFrame.

Comments