如何计算熊猫列中非空元素的数量?

时间:2020-07-03 12:21:29

标签: python-3.x pandas

如何获取pandas列中非空元素总数?

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

数据类型为int 我收到错误消息:

AttributeError:“ int”对象没有属性“ where”

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

2 个答案:

答案 0 :(得分:2)

如果为空意味着将空字符串与掩码进行比较,并将sum用作计数True的值:

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

如果为空则表示缺少值,请使用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

答案 1 :(得分:1)

替代答案:

以下代码使用正则表达式将NaN替换为空白。熊猫count用于非NA细胞。

# 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()

输出

print(counts)

Paid_Off_In_Days    4
dtype: int64