2017-03-05 37 views
1

說這個數據幀大熊貓據幀的風格,彰顯楠的

df = pd.DataFrame({'name' : ['A','B'], 'date' : pd.to_datetime(['2000-01-01','2000-01-02']), 'value' : [np.nan, 1]}) 

     date name value 
0 2000-01-01 A NaN 
1 2000-01-02 B 1.0 

我如何可以檢查哪些元素是nandf.applymap? (即,不使用df.isnull

問題來自我想使用熊貓html造型的地方。我們有內置的男突出

df.style.highlight_null() 

,但它改變了背景色,而不是我想要的「南」,以紅色顯示。

所以我需要applymap

df.style.applymap(lambda x: 'color: red' if isnan(x) else '') 

但我怎麼能檢查是否值是NaN,自己做的時候它也可以是日期時間/字符串? np.isnan將在字符串上失敗。 np.isreal(x) and np.isnan(x)也在日期時間失敗。

回答

3

您可以使用pd.isnull(),與更廣泛的交易類型缺失值檢查:

import pandas as pd 
df.style.applymap(lambda x: 'color: red' if pd.isnull(x) else '') 

enter image description here