2 回答

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊
您可以更改字典理解來(lái)過(guò)濾缺失值:
bar = {
? ? k: [d[k] for d in foo if k in d]
? ? for k in set().union(*foo)
}

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
首先轉(zhuǎn)換為列的字典:
from collections import defaultdict
bar = defaultdict(list)
for dict_ in foo:
for key, value in dict_.items():
bar[key].append(value)
那么它就變得微不足道了:
pd.DataFrame(bar)
# Out
# A B C
# 0 a1 b1 c1
# 1 a2 b2 c2
# 2 a3 b3 c3
添加回答
舉報(bào)