Skip to main content
 首页 » 编程设计

python之传递值的形状是 (2, 3),索引表示 (2, 1)

2025年05月04日24mfryf

答:

>>> pd.DataFrame({"a": [['*', 6.0689999999999884, 1, 0, [6.0689999999999884]], ['*', 6.0690000000000168, 1, 0, [6.0690000000000168]], ['*', 6.0689999999999884, 1,0, [6.0689999999999884]]], "b": 12}, index=[0, 1, 2]) 
                           a   b 
0  [*, 6.069, 1, 0, [6.069]]  12 
1  [*, 6.069, 1, 0, [6.069]]  12 
2  [*, 6.069, 1, 0, [6.069]]  12 

是的,创建了 3 行,但我想创建一行。

乙:

我尝试了以下方法,但它不起作用。

>>> pd.DataFrame({"a": [['*', 6.0689999999999884, 1, 0, [6.0689999999999884]], ['*', 6.0690000000000168, 1, 0, [6.0690000000000168]], ['*', 6.0689999999999884, 1,0, [6.0689999999999884]]], "b": 12}, index=[0]) 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
  File "/usr/lib64/python2.6/site-packages/pandas/core/frame.py", line 226, in __init__ 
    mgr = self._init_dict(data, index, columns, dtype=dtype) 
  File "/usr/lib64/python2.6/site-packages/pandas/core/frame.py", line 363, in _init_dict 
    dtype=dtype) 
  File "/usr/lib64/python2.6/site-packages/pandas/core/frame.py", line 5168, in _arrays_to_mgr 
    return create_block_manager_from_arrays(arrays, arr_names, axes) 
  File "/usr/lib64/python2.6/site-packages/pandas/core/internals.py", line 3916, in create_block_manager_from_arrays 
    construction_error(len(arrays), arrays[0].shape, axes, e) 
  File "/usr/lib64/python2.6/site-packages/pandas/core/internals.py", line 3882, in construction_error 
    passed,implied)) 
ValueError: Shape of passed values is (2, 3), indices imply (2, 1) 

C

它按照创建的列表列表的方式工作。

>>> pd.DataFrame({"a": [[['*', 6.0689999999999884, 1, 0, [6.0689999999999884]], ['*', 6.0690000000000168, 1, 0, [6.0690000000000168]], ['*', 6.0689999999999884, 1,0, [6.0689999999999884]]]], "b": 12}, index=[0]) 
                                                   a   b 
0  [[*, 6.069, 1, 0, [6.069]], [*, 6.069, 1, 0, [...  12 
>>>  

但是我的输入像上面的 B.

那怎么办?

请您参考如下方法:

如果 C 可以工作,那么,为什么不使用 C 呢? 当你说“我的输入像上面的 B”时,你的意思是你有一个变量,比方说,数据,像这样:

data = [['*', 6.0689999999999884, 1, 0, [6.0689999999999884]], ['*', 6.0690000000000168, 1, 0, [6.0690000000000168]], ['*', 6.0689999999999884, 1,0, [6.0689999999999884]]] 

?

那么,你为什么不简单地使用这个:

pd.DataFrame({"a": [data], "b": 12}, index=[0]) 

我不确定我是否完全理解你想要什么。