gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
with pandas in python, how do i count number of rows in a dataframe
- User
with pandas in python, how do i count number of rows in a dataframe
- Assistant
In pandas, you can count the number of rows in a DataFrame using the
shapeattribute or thelen()function. Here are the two methods:-
Using the
shapeattribute: Theshapeattribute returns a tuple representing the dimensions of the DataFrame, where the first element is the number of rows and the second element is the number of columns.pythonimport pandas as pd # Sample DataFrame data = {'A': [1, 2, 3], 'B': [4, 5, 6]} df = pd.DataFrame(data) # Count number of rows num_rows = df.shape[0] print("Number of rows:", num_rows) -
Using the
len()function: You can also use thelen()function to get the number of rows, which returns the length of the DataFrame.python# Count number of rows num_rows = len(df) print("Number of rows:", num_rows)
Both methods will give you the same result. You can use either based on your preference.
-