【发布时间】:2022-01-23 04:44:50
【问题描述】:
我有两个如下所示的数据框:
df1 = pd.DataFrame(
{
"A_price": [10, 12],
"B_price": [20, 21],
},
index = ['01-01-2020', '01-02-2021']
)
df1:
A_price B_price
01-01-2020 10 20
01-02-2021 12 21
df2 = pd.DataFrame(
{
"A_weight": [0.1, 0.12],
"B_weight": [0.2, 0.21],
},
index = ['01-01-2020', '01-02-2021']
)
df2:
A_weight B_weight
01-01-2020 0.1 0.2
01-02-2021 0.12 0.21
如何将两个数据框连接到相同的索引上,然后将列置于层次结构中?即我想要以下内容:
df:
A B
price weight price weight
01-01-2020 10 0.1 20 0.2
01-02-2021 12 0.12 21 0.21
【问题讨论】:
标签: python pandas dataframe join hierarchy