Pandas 将列索引级别添加到数据框
Pandas Add Column Index Level to Data Frame
给定以下数据框:
d2=pd.DataFrame({'Item':['y','y','z','x'],
'other':['aa','bb','cc','dd']})
d2
Item other
0 y aa
1 y bb
2 z cc
3 x dd
我想在现有索引下添加列索引级别 1(我认为),因为我想将此数据框连接到另一个多索引的数据框。
我不想更改其他数据框,因为我已经编写了很多假设其当前结构的代码。
提前致谢!
IIUC 你可以添加参数 append=True
到 set_index
:
print (d2.set_index('Item', append=True))
other
Item
0 y aa
1 y bb
2 z cc
3 x dd
给定以下数据框:
d2=pd.DataFrame({'Item':['y','y','z','x'],
'other':['aa','bb','cc','dd']})
d2
Item other
0 y aa
1 y bb
2 z cc
3 x dd
我想在现有索引下添加列索引级别 1(我认为),因为我想将此数据框连接到另一个多索引的数据框。 我不想更改其他数据框,因为我已经编写了很多假设其当前结构的代码。
提前致谢!
IIUC 你可以添加参数 append=True
到 set_index
:
print (d2.set_index('Item', append=True))
other
Item
0 y aa
1 y bb
2 z cc
3 x dd