Pandas。面板(data=list\ of\ dfs)坏了吗?

2024-04-25 01:00:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: nameimportnumpydataframeversionaswithnp
1条回答
网友
1楼 · 发布于 2024-04-25 01:00:14

根据文档,面板构造器只接受数据帧的ndarray或dict。您可能需要先将数据帧列表转换为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

相关问题 更多 >