python

2017-10-16 98 views
0

在excel表格中顯示none單元格我正在處理Excel表格和python的問題。我已成功檢索了由熊貓在Excel中的特定列和行。現在我只想顯示具有「無」或「空」值的行和列。 excel表的示例圖像enter image description herepython

在上圖中,我需要行和列的值爲無。例如在「estfix」列中有幾個沒有值,所以我需要檢查列值,如果它不是我需要打印它的相應的行和列。希望你能理解。

代碼我想:

import pandas as pd 
wb= pd.ExcelFile(r"pathname details.xlsx") 
sheet_1=pd.read_excel(r"pathname details.xlsx",sheetname=0) 

c=sheet_1[["bugid","sev","estfix","manager","director"]] 
print(c) 

我使用python 3.6。提前致謝!

我希望輸出這樣的:enter image description here

這裏楠被認爲是一個無。

回答

1

使用isnullany用於檢查至少一個真:

a = df[df.isnull().any(axis=1)] 

對於列與列:

b = df.loc[df.isnull().any(axis=1), df.isnull().any()] 

樣品:

df = pd.DataFrame({'A':list('abcdef'), 
        'B':[4,np.nan,4,5,5,4], 
        'C':[7,8,9,4,2,3], 
        'D':[1,3,np.nan,7,1,0], 
        'E':[5,3,6,9,2,4], 
        'F':list('aaabbb')}) 

print (df) 
    A B C D E F 
0 a 4.0 7 1.0 5 a 
1 b NaN 8 3.0 3 a 
2 c 4.0 9 NaN 6 a 
3 d 5.0 4 7.0 9 b 
4 e 5.0 2 1.0 2 b 
5 f 4.0 3 0.0 4 b 

a = df[df.isnull().any(1)] 
print (a) 
    A B C D E F 
1 b NaN 8 3.0 3 a 
2 c 4.0 9 NaN 6 a 

b = df.loc[df.isnull().any(axis=1), df.isnull().any()] 
print (b) 
    B D 
1 NaN 3.0 
2 4.0 NaN 

詳細信息:

print (df.isnull()) 
     A  B  C  D  E  F 
0 False False False False False False 
1 False True False False False False 
2 False False False True False False 
3 False False False False False False 
4 False False False False False False 
5 False False False False False False 

print (df.isnull().any(axis=1)) 
0 False 
1  True 
2  True 
3 False 
4 False 
5 False 
dtype: bool 

print (df.isnull().any()) 
A False 
B  True 
C False 
D  True 
E False 
F False 
dtype: bool