Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
     
like image 785
bibscy Avatar asked Jan 25 '26 11:01

bibscy


1 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
like image 142
jezrael Avatar answered Jan 27 '26 00:01

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!