英文:
Concatenate column levels to an existing multi-column pandas dataframe
问题
以下是翻译好的部分:
我有两个带有多列索引的数据框。这两个数据框的列数完全相同。
import pandas as pd
columns_df1 = pd.MultiIndex.from_tuples([
('A', 1, 'X', 'Y', 'Z'),
('B', 2, 'X', 'Y', 'Z'),
('C', 3, 'X', 'Y', 'Z')
], names=['level1', 'level2', 'level3', 'level4', 'level5'])
df1 = pd.DataFrame([[1, 2, 3]], columns=columns_df1)
columns_df2 = pd.MultiIndex.from_tuples([
('D', 4, 'P', 'Q', 'R'),
('E', 5, 'P', 'Q', 'R'),
('F', 6, 'P', 'Q', 'R')
], names=['level6', 'level7', 'level8', 'level9', 'level10'])
df2 = pd.DataFrame([[4, 5, 6]], columns=columns_df2)
print(df1)
print(df2)
我需要将“df2”的最后两个列级别(level9和level10)添加到“df1”。最佳方法是什么?
期望的结果:
level1 A B C
level2 1 2 3
level3 X X X
level4 Y Y Y
level5 Z Z Z
level9 Q Q Q
level10 R R R
0 1 2 3
英文:
I have two dataframes with a multi-column index. Both dataframes have exactly the same number of columns.
import pandas as pd
columns_df1 = pd.MultiIndex.from_tuples([
('A', 1, 'X', 'Y', 'Z'),
('B', 2, 'X', 'Y', 'Z'),
('C', 3, 'X', 'Y', 'Z')
], names=['level1', 'level2', 'level3', 'level4', 'level5'])
df1 = pd.DataFrame([[1, 2, 3]], columns=columns_df1)
columns_df2 = pd.MultiIndex.from_tuples([
('D', 4, 'P', 'Q', 'R'),
('E', 5, 'P', 'Q', 'R'),
('F', 6, 'P', 'Q', 'R')
], names=['level6', 'level7', 'level8', 'level9', 'level10'])
df2 = pd.DataFrame([[4, 5, 6]], columns=columns_df2)
print(df1)
print(df2)
level1 A B C
level2 1 2 3
level3 X X X
level4 Y Y Y
level5 Z Z Z
0 1 2 3
level6 D E F
level7 4 5 6
level8 P P P
level9 Q Q Q
level10 R R R
0 4 5 6
I need to add the last two column levels (level9 and level10) of df2
to df1
. What's the best way to do this?
Expected result:
level1 A B C
level2 1 2 3
level3 X X X
level4 Y Y Y
level5 Z Z Z
level9 Q Q Q
level10 R R R
0 1 2 3
答案1
得分: 4
一种选择是将这两个索引合并在一起:
last = 2
names = df1.columns.names.union(df2.columns.names[-last:])
df1.set_axis(pd.MultiIndex.from_tuples([i1 + i2[-last:] for i1,i2 in list(zip(df1.columns,df2.columns))],names = names),axis=1)
输出结果:
level1 A B C
level2 1 2 3
level3 X X X
level4 Y Y Y
level5 Z Z Z
level9 Q Q Q
level10 R R R
0 1 2 3
英文:
One option is to zip the two indexes together:
last = 2
names = df1.columns.names.union(df2.columns.names[-last:])
df1.set_axis(pd.MultiIndex.from_tuples([i1 + i2[-last:] for i1,i2 in list(zip(df1.columns,df2.columns))],names = names),axis=1)
Output:
level1 A B C
level2 1 2 3
level3 X X X
level4 Y Y Y
level5 Z Z Z
level9 Q Q Q
level10 R R R
0 1 2 3
答案2
得分: 0
使用pandas.MultiIndex.to_frame
/from_frame
和concat
将DataFrames用作中间件:
new_idx = pd.MultiIndex.from_frame(pd.concat([df1.columns.to_frame(index=False),
df2.columns.to_frame(index=False).iloc[:, -2:],
], axis=1))
out = df1.set_axis(new_idx, axis=1)
输出:
level1 A B C
level2 1 2 3
level3 X X X
level4 Y Y Y
level5 Z Z Z
level9 Q Q Q
level10 R R R
0 1 2 3
英文:
Using DataFrames as intermediates with pandas.MultiIndex.to_frame
/from_frame
and concat
:
new_idx = pd.MultiIndex.from_frame(pd.concat([df1.columns.to_frame(index=False),
df2.columns.to_frame(index=False).iloc[:, -2:],
], axis=1))
out = df1.set_axis(new_idx, axis=1)
Output:
level1 A B C
level2 1 2 3
level3 X X X
level4 Y Y Y
level5 Z Z Z
level9 Q Q Q
level10 R R R
0 1 2 3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论