2016-11-01 62 views
1

我有一個具有以下結構的數據幀:如何將我的DataFrame轉換爲字典而忽略NaN值?

 Question 0  1   2  3  4 5 6 7 8 
0  1  NaN Never Sometimes Usually Always NaN NaN NaN NaN 
1  2  NaN Never Sometimes Usually Always NaN NaN NaN NaN 
2  3  NaN Never Sometimes Usually Always NaN NaN NaN NaN 
3  4  NaN Never Sometimes Usually Always NaN NaN NaN NaN 

我想這個轉換成詞典的詞典。外部字典的鍵值從'問題'列中獲得。所以,我試過如下:

dictionary = lookup.set_index('Question').T.to_dict() 

我:

{1: {'0': nan, 
    '1': 'Never', 
    '2': 'Sometimes', 
    '3': 'Usually', 
    '4': 'Always', 
    '5': nan, 
    '6': nan, 
    '7': nan, 
    '8': nan, 
    '9': nan}, 
2: {'0': nan, 
    '1': 'Never', 
    '2': 'Sometimes', 
    '3': 'Usually', 
    '4': 'Always', 
    '5': nan, 
    '6': nan, 
    '7': nan, 
    '8': nan, 
    '9': nan} ...... 

如何刪除鍵和NaN值?所需的字典是:

{1: {'1': 'Never', 
    '2': 'Sometimes', 
    '3': 'Usually', 
    '4': 'Always'}, 
2: {'1': 'Never', 
    '2': 'Sometimes', 
    '3': 'Usually', 
    '4': 'Always'} ...... 

回答

2

環路,併產生每個系列(問題)的一個字典與na值下降:

{q: s.dropna().to_dict() for q, s in df.set_index('Question').iterrows()} 
2

您可以使用字典理解,在這裏你過濾掉非字符串值,如果他們的NaN(使用numpy.isnan):在你行

>>> from numpy import isnan, nan 
>>> d = {1: {'0': nan,             
...:   '1': 'Never',     
...:   '2': 'Sometimes', 
...:   '3': 'Usually', 
...:   '4': 'Always', 
...:   '5': nan, 
...:   '6': nan}, 
...:  2: {'0': nan, 
...:   '1': 'Never', 
...:   '2': 'Sometimes', 
...:   '3': 'Usually', 
...:   '4': 'Always', 
...:   '5': nan, 
...:   '6': nan}} 
>>> { k1: { k2: v for k2, v in d2.iteritems() if type(v) == str or not isnan(v) } 
     for k1, d2 in d.iteritems() } 
{1: {'1': 'Never', '2': 'Sometimes', '3': 'Usually', '4': 'Always'}, 
2: {'1': 'Never', '2': 'Sometimes', '3': 'Usually', '4': 'Always'}}