1 回答

TA貢獻1817條經驗 獲得超6個贊
將 concat 與 級別 和 in 索引以及列中的 2 個級別結合使用:MultiIndexzipyearMultiIndex
#convert columns to MultiIndex in index
df2 = df2.set_index([('parity','zip'),('parity','year')])
#created new MultiIndex in columns
df3.columns = pd.MultiIndex.from_product([df3.columns, ['new']])
df = pd.concat([df1, df2, df3],axis=1).rename_axis(['zip','year'])
print (df)
parity education parity \
NaN 0.0 1.0 2.0 0.0 1.0 parity
zip year
20194 1990 0.333333 0.000000 0.333333 0.333333 0.333333 0.666667 1.5
22204 1990 0.000000 0.000000 1.000000 0.000000 0.666667 0.333333 1.0
24060 1990 0.333333 0.333333 0.000000 0.333333 0.666667 0.333333 1.0
education
new new
zip year
20194 1990 1.5 0.666667
22204 1990 1.0 0.333333
24060 1990 1.0 0.333333
添加回答
舉報