Python 熊猫转座concat()

Python 熊猫转座concat(),python,pandas,Python,Pandas,如何转换由concat()返回的DataFrame 我得到: AttributeError: 'DataFrame' object has no attribute 'dtypes' ! RuntimeError: maximum recursion depth exceeded in cmp 如果我尝试: concat([df, df]).T.to_dict() 我得到: AttributeError: 'DataFrame' object has no attribute 'dtype

如何转换由
concat()
返回的
DataFrame

我得到:

AttributeError: 'DataFrame' object has no attribute 'dtypes' !
RuntimeError: maximum recursion depth exceeded in cmp
如果我尝试:

concat([df, df]).T.to_dict()
我得到:

AttributeError: 'DataFrame' object has no attribute 'dtypes' !
RuntimeError: maximum recursion depth exceeded in cmp

我认为这与索引中的
concat()
引入的重复项有关,但没有找到解决方法。

您可以使用
键指定分层索引:

In [288]: concatenated = concat([df,df], keys=['first', 'second'])

In [289]: print concatenated.T
   first      second    
       0   1       0   1
a      1   1       1   1
b     10  20      10  20
c     41  42      41  42

In [290]: print concatenated.T.to_dict().values()
[{'a': 1, 'c': 41, 'b': 10}, {'a': 1, 'c': 41, 'b': 10}, {'a': 1, 'c': 42, 'b': 20}, {'a': 1, 'c': 42, 'b': 20}]

你能解释一下你想要实现什么吗?我曾经用df.T.to_dict().values()对数据帧值进行unittest。参考最新的pandas版本能够支持索引重复,我相信在0.8.0之前情况并非如此。