2017-08-28 60 views
1

我需要從一個.json文件導入的數據集中額外添加一個要素。從熊貓數據框中提取字典值

這是什麼樣子:

f1 = pd.read_json('https://raw.githubusercontent.com/ansymo/msr2013-bug_dataset/master/data/v02/eclipse/short_desc.json') 

print(f1.head()) 


               short_desc 
1  [{'when': 1002742486, 'what': 'Usability issue... 
10  [{'when': 1002742495, 'what': 'API - VCM event... 
100  [{'when': 1002742586, 'what': 'Would like a wa... 
10000 [{'when': 1014113227, 'what': 'getter/setter c... 
100001 [{'when': 1118743999, 'what': 'Create Help Ind... 

從本質上說,我需要「SHORT_DESC」作爲列名,並用字符串值正下方填充它:「可用性問題...

到目前爲止,我已經試過如下:

f1['desc'] = pd.DataFrame([x for x in f1['short_desc']]) 

Wrong number of items passed 19, placement implies 1 

是否有一個簡單的方法來做到這一點,而不使用循環?有人能指出這個新手朝着正確的方向嗎?

回答

3

不要初始化數據幀並嘗試將其分配給列 - 列應該是pd.Series

你剛纔應該直接分配列表中理解,就像這樣:

f1['desc'] = [x[0]['what'] for x in f1['short_desc']] 

作爲替代,我想提出一個解決方案不涉及任何lambda函數,使用operatorpd.Series.apply

import operator 

f1['desc'] = f1.short_desc.apply(operator.itemgetter(0))\ 
          .apply(operator.itemgetter('what')) 
print(f1.desc.head()) 

1   Usability issue with external editors (1GE6IRL) 
10     API - VCM event notification (1G8G6RR) 
100  Would like a way to take a write lock on a tea... 
10000  getter/setter code generation drops "F" in "..... 
100001 Create Help Index Fails with seemingly incorre... 
Name: desc, dtype: object 
+0

這就是讓我瘋狂的原因,爲什麼我們得到了1,10,100等等,沒有'short_desc'和列標題。 – JohnWayne360

+0

@ JohnWayne360因爲你正在打印一系列作品。嘗試'print(df.head())'。你會得到它。 –

+0

@ JohnWayne360有趣的是,當你從網頁鏈接加載它時,該索引似乎就出現了。想要重置它?做'f1 = f1.reset_index(drop = 1)' –

2

或者您可以嘗試apply(PS:apply考慮作爲時間成本函數)

f1['short_desc'].apply(pd.Series)[0].apply(pd.Series) 

Out[864]: 
                what  when who 
1   Usability issue with external editors (1GE6IRL) 1002742486 21 
10     API - VCM event notification (1G8G6RR) 1002742495 10 
100  Would like a way to take a write lock on a tea... 1002742586 24 
10000 getter/setter code generation drops "F" in "..... 1014113227 331 
100001 Create Help Index Fails with seemingly incorre... 1118743999 9571 
+1

謝謝,如果/當我試圖將'when'和'what'匹配起來,這個答案對我有用。非常感激! – JohnWayne360

+0

@ JohnWayne360不客氣 – Wen