pandas.Panel(data = list_of_dfs)坏了吗?

时间:2016-04-05 08:36:02

标签: python numpy pandas panel

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

print(np.__version__)
print(pd.__version__)

d1 = pd.DataFrame({'Name': [1, 1, 1, 1, 1],'number': [1, 1, 1, 1, 1]})
d2 = pd.DataFrame({'Name': [1, 1, 1, 1, 1], 'number': [1, 1, 1, 1, 1]}) 

x=[d1,d2]
pd.Panel(x, items=[1,2])

错误:

<python dir>\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
    472 
    473     """
--> 474     return array(a, dtype, copy=False, order=order)
    475 
    476 def asanyarray(a, dtype=None, order=None):

ValueError: cannot copy sequence with size 5 to array axis with dimension 2

我认为这与以下帖子有关,但我不确定最佳解决方法是什么:ValueError: cannot copy sequence with size 5 to array axis with dimension 2

1 个答案:

答案 0 :(得分:3)

根据文档,Panel构造函数只接受数据帧的ndarray或dict。您可能希望首先将DataFrame列表转换为dict。

import numpy as np
import pandas as pd

print(np.__version__)
print(pd.__version__)

d1 = pd.DataFrame({'Name': [1, 1, 1, 1, 1],'number': [1, 1, 1, 1, 1]})
d2 = pd.DataFrame({'Name': [1, 1, 1, 1, 1], 'number': [1, 1, 1, 1, 1]})

x = dict(enumerate([d1,d2], 1))
pd.Panel(x)

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Panel.html#pandas.Panel