(x,) 在 NumPy 形状中表示什么?

我一直在努力寻找 (x,) 在 NumPy 形状中究竟表示什么?从外观上看,我知道数组中有“x”个列/元素,这基本上是一个一维数组。

但我的问题是这里的 x 后面的逗号 (x,) 表示什么?我问这个问题是因为,我正在尝试创建一个 DataFrame 并且它给了我一个错误:

ValueError: Shape of passed values is (3, 1), indices imply (1, 3)

我的代码:

price = np.array([10, 8, 12])

df_price = pd.DataFrame(price, 
                        index=(["Price"]),
                        columns=(["Almond Butter","Peanut Butter", "Cashew Butter"]))

谁能告诉我为什么这个“价格”数组的形状在这里是 (3,1)?它不是。它是 (3,) -- 就是这样。


回首忆惘然
浏览 147回答 3
3回答

HUWWW

当尝试从平面数组创建 Pandas DataFrame 时,数组必须转换为某种二维形式,因为 Pandas DataFrame 几乎总是二维的。出现这个问题是因为你有一行三列,所以数据数组的形状应该是(1, 3). 构造函数pd.DataFrame必须在数组末尾添加一个维度,并假定第一个维度中的每个项目都是 DataFrame 中的一行。一个简单的解决方法是将数据数组重塑为行数乘以列数。price = np.array([10, 8, 12]).reshape(1, -1)上面调用-1中的.reshape告诉函数推断该轴的长度。

凤凰求蛊

我的问题是这里的 x 后面的逗号 (x,) 表示什么?此语法是通用的 Python,并不特定于 Numpy。当我们要创建一个元组时,我们在这种情况下添加一个逗号。您应该熟悉元组,例如(3, 4). 但是,如果我们想创建一个只有一个元素的元组怎么办。您可以尝试(3),但现在 Python 将括号解释为数学表达式中的分组运算符,就像我们使用它们时一样(3 + 4) * 5。这意味着它(3)只是整数值3,而不是元组。所以我们添加一个逗号(3,)来创建一个只有一个元素的元组。

慕丝7291255

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

相关分类

Python