Python-pandas.DataFrame-找出有空值的行

0.摘要python

pandas中DataFrame类型中,找出全部有空值的行,能够使用.isnull()方法和.any()方法。code

 

1.找出含有空值的行blog

方法:DataFrame[DataFrame.isnull().T.any()]pandas

其中,isnull()可以判断数据中元素是否为空值;T为转置;any()判断该行是否有空值。class

import pandas as pd
import numpy as np

n = np.arange(20, dtype=float).reshape(5,4)
n[2,3] = np.nan
index = ['index1', 'index2', 'index3', 'index4', 'index5']
columns = ['column1', 'column2', 'column3', 'column4']
frame3 = pd.DataFrame(data=n, index=index, columns=columns)
print(frame3[frame3.isnull().T.any()])

程序成功找到了第三行为有空值的行。import

 

2.为何加转置float

在代码中,isnull()的结果须要求转置以后,才能进行any()操做,这是为何呢?程序

下面对比一下isnull转置和非转置的状况:numpy

print(frame3.isnull().any())
print("========================")
print(frame3.isnull().T.any())

可见:方法

非转置:frame3.isnull().any(),获得的每一列求any()计算的结果,输出为列的Series。

转置:frame3.isnull().T.any(),获得的每一行求any()计算的结果,输出为行的Series。