我有一个数据帧,目前看起来像这样:
country series year value
usa a 2010 21
usa b 2015 22
usa a 2017 23
usa b 2010 22
usa b 2017 23
aus a 2010 21
aus b 2015 22
aus a 2017 23
aus b 2010 22
aus b 2017 23
当我运行这段代码时,它减少了国家的两面性,但不像我期望的那样减少了序列。你知道吗
pop2.set_index(['Country','Series'])
我想要:
country series year value
usa a 2010 21
2017 23
b 2010 22
2015 22
2017 23
aus a 2010 21
2017 23
b 2010 22
2015 22
2017 23
相反,它正在返回:
country series year value
usa a 2010 21
b 2015 22
a 2017 23
b 2010 22
b 2017 23
aus a 2010 21
b 2015 22
a 2017 23
b 2010 22
b 2017 23
在数据帧中显示的每一行必须有一个索引标签。因此,您需要的是另一个级别的索引,然后您可以根据需要显示索引“grouping”。你知道吗
让我们试试这个:
输出:
相关问题 更多 >
编程相关推荐