In Pandas, You can get the count of each row of DataFrame using DataFrame.count()
method. In order to get the row count you should use axis='columns'
as an argument to the count() method. Note that the count() method ignores all None & nan values from the count.
# Syntax of df.count()
df.count(axis='columns')
Let’s see with an example
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
'Courses Fee' :[22000,25000,23000,24000,26000],
'Duration':['30days','50days','30days', None,np.nan],
'Discount':[1000,2300,1000,1200,2500]
}
df = pd.DataFrame(technologies)
print(df)
Yields below output.
# Output:
Courses Courses Fee Duration Discount
0 Spark 22000 30days 1000
1 PySpark 25000 50days 2300
2 Hadoop 23000 30days 1000
3 Python 24000 None 1200
4 Pandas 26000 NaN 2500
1. Pandas Get Count of Each DataFrame Row Example
Now, let’s run the DatFrame.count()
to get the count of each row by ignoring None
and Nan
values.
# Pandas Get Count of Each DataFrame Row Example
df.count(axis='columns')
Yields below output. Note that Rows 3 and 4 are 3 as these two rows have None or Nan values.
# Output:
0 4
1 4
2 4
3 3
4 3
Happy Learning !!
Related Articles
- How to Get the Row Count From DataFrame
- Change Column Data Type On Pandas DataFrame
- Pandas Drop the First Row of DataFrame
- Get Unique Rows in Pandas DataFrame
- Get First N Rows of Pandas DataFrame
- Pandas Get Row Number of DataFrame
- Pandas Get Last Row from DataFrame?
- Pandas Count Unique Values in Column
- Pandas Count Distinct Values DataFrame
- Pandas Count The Frequency of a Value in Column
- Pandas DataFrame isna() function