慕丝7291255
错误的完整回溯表明已经DataFrame对您的输入进行了相当多的处理。In [336]: pd.DataFrame(np.arange(1,4), ...: index=(["Price"]), ...: columns=(["Almond Butter","Peanut Butter", "Cashew Butter"])) ---------------------------------------------------------------------------ValueError Traceback (most recent call last)/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py in create_block_manager_from_blocks(blocks, axes) 1653 blocks = [-> 1654 make_block(values=blocks[0], placement=slice(0, len(axes[0]))) 1655 ]/usr/local/lib/python3.6/dist-packages/pandas/core/internals/blocks.py in make_block(values, placement, klass, ndim, dtype) 3052 -> 3053 return klass(values, ndim=ndim, placement=placement) 3054 /usr/local/lib/python3.6/dist-packages/pandas/core/internals/blocks.py in __init__(self, values, placement, ndim) 124 raise ValueError(--> 125 f"Wrong number of items passed {len(self.values)}, " 126 f"placement implies {len(self.mgr_locs)}"ValueError: Wrong number of items passed 1, placement implies 3During handling of the above exception, another exception occurred:ValueError Traceback (most recent call last)<ipython-input-336-43d59803fb0f> in <module> 1 pd.DataFrame(np.arange(1,4), 2 index=(["Price"]),----> 3 columns=(["Almond Butter","Peanut Butter", "Cashew Butter"]))/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy) 462 mgr = init_dict({data.name: data}, index, columns, dtype=dtype) 463 else:--> 464 mgr = init_ndarray(data, index, columns, dtype=dtype, copy=copy) 465 466 # For data is list-like, or Iterable (will consume into list)/usr/local/lib/python3.6/dist-packages/pandas/core/internals/construction.py in init_ndarray(values, index, columns, dtype, copy) 208 block_values = [values] 209 --> 210 return create_block_manager_from_blocks(block_values, [columns, index]) 211 212 /usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py in create_block_manager_from_blocks(blocks, axes) 1662 blocks = [getattr(b, "values", b) for b in blocks] 1663 tot_items = sum(b.shape[0] for b in blocks)-> 1664 construction_error(tot_items, blocks[0].shape[1:], axes, e) 1665 1666 /usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py in construction_error(tot_items, block_shape, axes, e) 1692 if block_shape[0] == 0: 1693 raise ValueError("Empty data passed with indices specified.")-> 1694 raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}") 1695 1696 ValueError: Shape of passed values is (3, 1), indices imply (1, 3)如果我们不指定索引,它会生成一维列框:In [337]: pd.DataFrame(np.arange(1,4)) # (3,) input Out[337]: 00 11 22 3与 (3,1) 输入相同:In [339]: pd.DataFrame(np.arange(1,4)[:,None]) # (3,1) input Out[339]: 00 11 22 3但你想要一个(1,3):In [340]: pd.DataFrame(np.arange(1,4)[None,:]) # (1,3) input Out[340]: 0 1 20 1 2 3numpy广播可以将 (3,) 数组扩展为 (1,3),但这不是它DataFrame正在做的事情。根据您的看法,pandas 数据框可能看起来像是 2d numpy 数组的转置。系列是 1d,但垂直显示。数据框索引优先考虑列。在探索底层数据存储和values/to_numpy(). 细节很复杂。请注意,回溯讨论了“block_manager”等。In [342]: pd.Series(np.arange(1,4)) Out[342]: 0 11 22 3dtype: int64