您好,有一个多列数据框,并且想要打印列行的总和,如果这些行不是 nan
或 -
或 ?
Col1 Col2 Col3
this nan 1
that ? 2
those this 5
these that 2
there - 1
我想要的结果是
print df['Col2].count() # Im missing the not ( nan or ? or - ) part here
2
最佳答案
由 Series.notna
创建的链罩通过 Series.isin
进行反向检查成员资格:
mask = df['Col2'].notna() & ~df['Col2'].isin(['?','-', 'nan'])
#if no missing values only strings nan
#mask = ~df['Col2'].isin(['?','-', 'nan'])
print (mask.sum())
2
详细信息:
print (mask)
0 False
1 False
2 True
3 True
4 False
Name: Col2, dtype: bool
关于python - 如果没有某些特定值,则计算数据帧列中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59156140/