(或...我只是編輯列表的列表)轉動不規則詞典列表爲大熊貓數據框
是否有轉換這樣
food2 = {}
food2["apple"] = ["fruit", "round"]
food2["bananna"] = ["fruit", "yellow", "long"]
food2["carrot"] = ["veg", "orange", "long"]
food2["raddish"] = ["veg", "red"]
的結構到現有的Python /大熊貓方法像這樣的數據透視表?
+---------+-------+-----+-------+------+--------+--------+-----+
| | fruit | veg | round | long | yellow | orange | red |
+---------+-------+-----+-------+------+--------+--------+-----+
| apple | 1 | | 1 | | | | |
+---------+-------+-----+-------+------+--------+--------+-----+
| bananna | 1 | | | 1 | 1 | | |
+---------+-------+-----+-------+------+--------+--------+-----+
| carrot | | 1 | | 1 | | 1 | |
+---------+-------+-----+-------+------+--------+--------+-----+
| raddish | | 1 | | | | | 1 |
+---------+-------+-----+-------+------+--------+--------+-----+
天真地,我可能只是通過字典循環。我看到我如何在每個內部列表上使用地圖,但我不知道如何將它們加入/堆疊在字典上。一旦我做了加入他們的行列,我可以只使用pandas.pivot_table
for key in food2:
attrlist = food2[key]
onefruit_pairs = map(lambda x: [key, x], attrlist)
one_fruit_frame = pd.DataFrame(onefruit_pairs, columns=['fruit', 'attr'])
print(one_fruit_frame)
fruit attr
0 bananna fruit
1 bananna yellow
2 bananna long
fruit attr
0 carrot veg
1 carrot orange
2 carrot long
fruit attr
0 apple fruit
1 apple round
fruit attr
0 raddish veg
1 raddish red
優秀。對於成千上萬的「水果」和數以千計的屬性,你有沒有直覺去理解它的表現(與一些尚未明確的熊貓魔法相比)? –
我「有」導入itertools –
此解決方案爲簡單而不是性能優化。有很大的改進空間,特別是如果你事先知道屬性的話。更新爲缺少「導入」。 –