選項1
對齊兩個軸
隨着functools.partial
from functool import partial
(_, dfA), (dfC, dfB) = list(map(
partial(dfC.align, fill_value=0),
dfA.align(dfB, fill_value=0)
))
方案1B
對齊列僅
from functools import partial
(_, dfA), (dfC, dfB) = list(map(
partial(dfC.align, fill_value=0, axis=1),
dfA.align(dfB, fill_value=0, axis=1)
))
選項2
對齊兩個軸
隨着pd.DataFrame.reindex
from functools import reduce
lod = [dfA, dfB, dfC]
idx = reduce(pd.Index.union, (d.index for d in lod))
col = reduce(pd.Index.union, (d.columns for d in lod))
dfA, dfB, dfC = (d.reindex(idx, col, fill_value=0) for d in lod)
選項2B
對齊列僅
lod = [dfA, dfB, dfC]
col = reduce(pd.Index.union, (d.columns for d in lod))
dfA, dfB, dfC = (d.reindex(columns=col, fill_value=0) for d in lod)
設置
dfA = pd.DataFrame(**{
'columns': ['item', 'a', 'A'],
'data': [['A', 1, 2], ['B', 1, 3], ['C', 0, 4]],
'index': [0, 1, 2]})
dfB = pd.DataFrame(**{
'columns': ['item', 'a', 'B'],
'data': [['E', 1, 2], ['F', 0, 6]],
'index': [0, 1]})
dfC = pd.DataFrame(**{
'columns': ['item', 'a', 'C'],
'data': [['G', 1, 3], ['H', 0, 4]],
'index': [0, 1]})
https://開頭計算器。com/questions/39050539 /添加多列到熊貓 - 同時希望鏈接將有所幫助 – Wen