bea*_*rdc 14 python group-by multi-index dataframe pandas
如果我有以下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
Run Code Online (Sandbox Code Playgroud)
我可以通过名字和目的地来集合......
>>> 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
Run Code Online (Sandbox Code Playgroud)
但我想要一个新的多索引数据框,看起来像
Length
Alice Athens 3
Rome 1
Rome 5
Bob Athens 3
Athens 2
Rome 5
Run Code Online (Sandbox Code Playgroud)
似乎应该有办法做一些像Dataframe(grouped)我的多索引Dataframe,但我得到一个PandasError("DataFrame构造函数没有正确调用!").
最简单的方法是什么?此外,任何人都知道是否会有一个选项将groupby对象传递给构造函数,或者我是否只是做错了?
谢谢
Gar*_*ett 13
由于您没有聚合类似的索引行,因此请尝试使用列名列表设置索引.
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
Run Code Online (Sandbox Code Playgroud)