pandas 如何从两个列表中创建一个dataframe?

14ifxucb  于 2023-04-04  发布在  其他
关注(0)|答案(1)|浏览(169)

此问题在此处已有答案

ValueError: Shape of passed values is (1, 6), indices imply (6, 6)(4个答案)
pandas dataframe Shape of passed values is (1, 4), indices imply (4, 4)(1个答案)
4天前关闭。
我有两个列表,两个列表都有相同的元素数(每个48)。我想创建一个dataframe,其中一个列表作为列,另一个列表作为所述列的值。像下面的例子:

list_1 = ["A", "B", "C", "D", "E"]
list_2 = [1, 2, 3, 4, 5]

我希望得到的dataframe是这样的:
| A|B|C|D|E级|
| --------------|--------------|--------------|--------------|--------------|
| 1|第二章|三|四|五|
我错过了什么?有更好/更简单的方法吗?
我试过使用pandas:

dataframe = pd.DataFrame(data=data, columns=fields)

但我得到了以下错误:

Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[83], line 1
----> 1 dataframe_maybe = pd.DataFrame(data=data, columns=fields)
      3 dataframe_maybe

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pandas\core\frame.py:762, in DataFrame.__init__(self, data, index, columns, dtype, copy)
    754         mgr = arrays_to_mgr(
    755             arrays,
    756             columns,
   (...)
    759             typ=manager,
    760         )
    761     else:
--> 762         mgr = ndarray_to_mgr(
    763             data,
    764             index,
    765             columns,
    766             dtype=dtype,
    767             copy=copy,
    768             typ=manager,
    769         )
    770 else:
    771     mgr = dict_to_mgr(
    772         {},
...
    418 passed = values.shape
    419 implied = (len(index), len(columns))
--> 420 raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}")

ValueError: Shape of passed values is (48, 1), indices imply (48, 48)
8zzbczxx

8zzbczxx1#

只要做pd.DataFrame([list_2], columns=list_1)
您的数据需要像ndarray一样

相关问题