Python 计算 Dataframe 每一列中非 NaN 条目的数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29971075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count number of non-NaN entries in every column of Dataframe
提问by cryp
I have a really big DataFrame and I was wondering if there was short (one or two liner) way to get the a count of non-NaN entries in a DataFrame. I don't want to do this one column at a time as I have close to 1000 columns.
我有一个非常大的 DataFrame,我想知道是否有短(一个或两个班轮)方法来获取 DataFrame 中非 NaN 条目的数量。我不想一次做一列,因为我有近 1000 列。
df1 = pd.DataFrame([(1,2,None),(None,4,None),(5,None,7),(5,None,None)],
columns=['a','b','d'], index = ['A', 'B','C','D'])
a b d
A 1 2 NaN
B NaN 4 NaN
C 5 NaN 7
D 5 NaN NaN
Output:
输出:
a: 3
b: 2
d: 1
回答by Alex Riley
回答by hemanta
If you want to sum the total count values which are not NAN, one can do;
如果要对非 NAN 的总计数值求和,可以这样做;
np.sum(df.count())