2017-10-18 16 views
2

有一個有趣的問題,即存在以下格式的詞典:清單DF(不等長元組)

test = {'A': [(1,2),(3,4)], 
     'B': [(1,2),(5,6),(7,8)]} 

對於字典中的每個關鍵,他們(第一次總是)共享一些值,但是每個列表中都有不同數量的元組。

最終,我的目標是代表了該格式的數據:

 1 3 5 7 
    A 2 4 - - 
    B 2 - 6 8 

有沒有在字典中的值,這些共同的元素轉換爲數據幀的一列一個聰明的辦法?

我想拿到名單的長度相等的是以下幾點:

#get all the unique first elements of the dictionary 
unique=[] 
for i in test.values(): 
    for j in i: 
     unique.append(j[0]) 

unique = set(unique) 

values_of_A = test['A'] 

#I thought this would loop through each tuple in the list and check if its 
#first value is in the list of unique elements; otherwise, it will print 
#0. However, it comes up with a blank list... 
full_list = [0 for i, v in enumerate(values_of_A) if v[0] not in unique] 

感謝一如既往!

+1

你能解釋一下多一點關於你如何想'test'的數據進行轉換?目前還不清楚生產產出的邏輯是什麼。 – ASGM

+0

是的 - 只是試圖掃描所有的元組,並將所有元組中的第一個元素作爲列標題放在第一個位置,然後將字典鍵作爲第一個列,並且表的值將是與比賽。不知道這是否有幫助,但也許上面的圖片有助於用詞。如果沒有,請告訴我。無論哪種方式,juanpa的解決方案都是完美的! – user6142489

+0

很高興聽到@ juanpa.arrivillaga的解決方案奏效 - 但請注意,您指定的輸出與其不匹配。 (這不是對我提出的答案的批評)。 – ASGM

回答

7

你可能只Munge時間,並使用from_dict替代構造函數:

>>> pd.DataFrame.from_dict({k:dict(v) for k,v in test.items()}, orient='index') 
    1 3 5 7 
A 2 4.0 NaN NaN 
B 2 NaN 6.0 8.0 
>>> 
+0

令人驚訝的高效率,正是我想要的。謝謝! – user6142489

+1

@ user6142489歡迎您。請注意,將來您可以直接構建一個集合,如'unique = set()',然後在for循環中'unique.add(j [0])' –

+0

完成!再次感謝! – user6142489