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'} ......