Pandas Get Count of Each Row of DataFrame

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.


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.


   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

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.


df.count(axis='columns')

Yields below output. Note that Rows 3 and 4 are 3 as these two rows have None or Nan values.


0    4
1    4
2    4
3    3
4    3

Happy Learning !!

You May Also Like

References

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

Pandas Get Count of Each Row of DataFrame