0

我想在一个索引中设置两列及其索引。但我无法合并表索引。我如何使用 pandas 或行 python 代码合并表索引?

我试过得到这个https://ibb.co/7nZyxCM

这是使用 PrettyTable https://ibb.co/Hh80LBJ的示例代码

我想得到什么: https ://ibb.co/cQWf2Rz

4

1 回答 1

1

您可以创建新的 MultiIndex(用于列),例如从元组:

cols = pd.MultiIndex.from_tuples([
    ('August', 'Invoice'), ('August', 'Sells'),
    ('September', 'Invoice'), ('September', 'Sells'),
    ('Growth', '1'),  ('Growth', '2') ])

然后只需将其设置为df中的

df.columns = cols
于 2019-10-27T05:23:46.680 回答