且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python/Pandas:计算每行中缺失/NaN 的数量

更新时间:2022-12-10 09:50:21

你可以先通过 isnull() 判断元素是否为 NaN 然后取行-明智的sum(axis=1)

You could first find if element is NaN or not by isnull() and then take row-wise sum(axis=1)

In [195]: df.isnull().sum(axis=1)
Out[195]:
0    0
1    0
2    0
3    3
4    0
5    0
dtype: int64

而且,如果你想要输出为列表,你可以

And, if you want the output as list, you can

In [196]: df.isnull().sum(axis=1).tolist()
Out[196]: [0, 0, 0, 3, 0, 0]

或者使用 count 之类的

In [130]: df.shape[1] - df.count(axis=1)
Out[130]:
0    0
1    0
2    0
3    3
4    0
5    0
dtype: int64