【发布时间】:2016-10-17 00:08:46
【问题描述】:
我有一个数据框,并将索引设置为数据框的一列。这将创建一个分层列索引。我想将列展平为一个级别。类似于这个问题 - Python Pandas - How to flatten a hierarchical index in columns,但是,列不重叠(即“id”不在分层索引的第 0 级,而其他列在索引的第 1 级)。
df = pd.DataFrame([(101,3,'x'), (102,5,'y')], columns=['id', 'A', 'B'])
df.set_index('id', inplace=True)
A B
id
101 3 x
102 5 y
所需的输出是扁平列,如下所示:
id A B
101 3 x
102 5 y
【问题讨论】: