How to count number of non empty elements in column in pandas?

Viewed 2995

How can i get the count of total non empty elements in a pandas column?

print(len(newDF.Paid_Off_In_Days).where(newDF.Paid_Off_In_Days != ''))

Data type is int I get error:

AttributeError: 'int' object has no attribute 'where'

  Paid_Off_In_Days        Credit_Amount
     1                       150
     15                      500
                             80
     18                      90
                             1200
     29                      600
     
2 Answers

If empty means empty string compare for mask and use sum for count True values:

print((newDF.Paid_Off_In_Days != '').sum())

If empty means missing value use Series.count:

print (newDF)
   Paid_Off_In_Days  col
0               1.0    a
1              15.0    s
2               NaN    d
3              18.0  NaN
4               NaN    f
5              29.0  NaN

print(newDF.Paid_Off_In_Days.count())
4

Alternative answer:

Code below uses regex to replace blanks with NaN. And pandas count for non-NA cells.

# Import library
import pandas as pd

# Create DataFrame
newDF = pd.DataFrame({
    'Paid_Off_In_Days':[1, np.nan, 15, '   ', 18, 29]   
})

# Regex to replace blanks with NaN
newDF = newDF.replace(r'^\s*$', np.nan, regex=True)

# Get counts
counts = newDF.count()

Output

print(counts)

Paid_Off_In_Days    4
dtype: int64
Related