我有两个数据帧:
dfA = pd.DataFrame({
"Run": (
1, 1, 1, 2, 2, 2, 3, 3, 3
),
"Point": (
1, 2, 3, 1, 2, 3, 1, 2, 3
),
"Val": (
78, 79, 77, 78, 79, 77, 78, 79, 77
)
})
和
dfB = pd.DataFrame({
"Run": (
1, 1, 1, 2, 2, 2, 3, 3, 3,
),
"Point": (
1, 2, 3, 1, 2, 3, 1, 2, 3,
),
"Val": (
68, 69, 67, 68, 69, 67, 68, 69, 67,
),
})
具有
res = dfA.compare(dfB, keep_equal=True, keep_shape=True).rename(columns={'self': 'A', 'other': 'B'}, level=-1)
我得到了一个与我想要的结果完全一致的数据帧,但Dash AG Grid似乎无法处理多索引列数据帧,因此我想将“res”df压平以获得以下结构
end_df = pd.DataFrame({
"Run_A": (
1, 1, 1, 2, 2, 2, 3, 3, 3,
),
"Run_B": (
1, 1, 1, 2, 2, 2, 3, 3, 3,
),
"Point_A": (
1, 2, 3, 1, 2, 3, 1, 2, 3,
),
"Point_B": (
1, 2, 3, 1, 2, 3, 1, 2, 3,
),
"Val_A": (
78, 79, 77, 78, 79, 77, 78, 79, 77
),
"Val_B": (
68, 69, 67, 68, 69, 67, 68, 69, 67,
),
})
归档所需结构的最有效方法是什么?