How to get the row count of a Pandas DataFrame

.shape
method to get row count ofDataframe

.len(DataFrame.index)
fastest method to get row count in Pandas 
dataframe.apply()
to count rows that satisfy a condition in Pandas
We will introduce how to get the row count of a Pandas dataframe
, with different methods like shape
and len(DataFrame.index)
. There are notable performance differences we found that len(DataFrame.index)
is the fastest.
We also look at how we can use dataframe.apply()
to get how many elements of rows satisfies a condition or not.
.shape
method to get row count of Dataframe
Suppose df
is our dataframe
, for row count:
# python 3.x
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(15).reshape(3,5))
print(df)
print('Row count is:',df.shape[0])
Output:
0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
Row count is: 3
For columns count, we can use df.shape[1]
.
.len(DataFrame.index)
fastest method to get row count in Pandas
We can calculate rows in the Dataframe
by getting the length index of member variables:
# python 3.x
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(15).reshape(3,5))
print(df)
print('Row count is:',len(df.index))
Output:
0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
Row count is: 3
we can also pass df.axes[0]
instead of df.index
:
# python 3.x
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(15).reshape(3,5))
print(df)
print('Row count is:',len(df.axes[0]))
Output:
0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
Row count is: 3
For columns count we can use df.axes[1]
.
dataframe.apply()
to count rows that satisfy a condition in Pandas
By counting the number of True
in the returned series of the result dataframe.apply()
, we can get the elements of rows in dataframe
that satisfies the condition.
# python 3.x
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(15).reshape(3,5))
counterFunc = df.apply(
lambda x: True if x[1] > 3 else False , axis=1)
numOfRows = len(counterFunc[counterFunc == True].index)
print(df)
print('Row count > 3 in column[1]is:',numOfRows)
Output:
0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
Row count > 3 in column[1]is: 2
We are getting the number of rows whose value in column[1]
is greater than 3.