将 Pandas group by object 转换为多索引 Dataframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14301913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:35:24  来源:igfitidea点击:

Convert pandas group by object to multi-indexed Dataframe

pythongroup-bydataframepandasmulti-index

提问by beardc

If I have the following Dataframe

如果我有以下数据框

>>> df = pd.DataFrame({'Name': ['Bob'] * 3 + ['Alice'] * 3, \
'Destination': ['Athens', 'Rome'] * 3, 'Length': np.random.randint(1, 6, 6)}) 
>>> df    
  Destination  Length   Name
0      Athens       3    Bob
1        Rome       5    Bob
2      Athens       2    Bob
3        Rome       1  Alice
4      Athens       3  Alice
5        Rome       5  Alice

I can goup by name and destination...

我可以按名称和目的地分组...

>>> grouped = df.groupby(['Name', 'Destination'])
>>> for nm, gp in grouped:
>>>     print nm
>>>     print gp
('Alice', 'Athens')
  Destination  Length   Name
4      Athens       3  Alice
('Alice', 'Rome')
  Destination  Length   Name
3        Rome       1  Alice
5        Rome       5  Alice
('Bob', 'Athens')
  Destination  Length Name
0      Athens       3  Bob
2      Athens       2  Bob
('Bob', 'Rome')
  Destination  Length Name
1        Rome       5  Bob

but I would like a new multi-indexed dataframe out of it that looks something like

但我想要一个新的多索引数据框,看起来像

                Length
Alice   Athens       3
        Rome         1
        Rome         5
Bob     Athens       3
        Athens       2
        Rome         5

It seems there should be a way to do something like Dataframe(grouped)to get my multi-indexed Dataframe, but instead I get a PandasError("DataFrame constructor not properly called!").

似乎应该有一种方法来Dataframe(grouped)获取我的多索引数据帧,但我得到了一个PandasError(“数据帧构造函数未正确调用!”)。

What is the easiest way to get this? Also, anyone know if there will ever be an option to pass a groupby object to the constructor, or if I'm just doing it wrong?

获得这个的最简单方法是什么?另外,有人知道是否有将 groupby 对象传递给构造函数的选项,或者我是否只是做错了?

Thanks

谢谢

采纳答案by Garrett

Since you're not aggregating similarly indexed rows, try setting the index with a list of column names.

由于您没有聚合类似索引的行,请尝试使用列名列表设置索引。

In [2]: df.set_index(['Name', 'Destination'])
Out[2]: 
                   Length
Name  Destination        
Bob   Athens            3
      Rome              5
      Athens            2
Alice Rome              1
      Athens            3
      Rome              5