【发布时间】:2018-05-22 19:06:23
【问题描述】:
我有这段代码:
def feat_ext_nir(x):
img = loadImage(x['image_name'])
compressed_img = compress_image(16, 16, img[:,:,3])
return compressed_image.ravel()
cloud_feat_nir = cloud_df_samp.apply(feat_ext_nir, axis=1)
cloud_feat_nir.describe()
执行时出现此错误:
ValueError: Shape of passed values is (2000, 256), indices imply (2000, 5)
apply 函数应该返回一个新的数据帧。它从旧数据框 (cloud_df_samp) 获得的唯一信息是“image_name”列。 256 是应该返回的正确列数,但似乎认为该数字应该与 cloud_df_samp (5) 中的列数相同,我不知道为什么。
谁能告诉我为什么它需要 5 列或者我应该做些什么不同的事情?完整的错误跟踪是:
ValueError: Shape of passed values is (2000, 256), indices imply (2000, 5)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/pandas/core/internals.py in
create_block_manager_from_arrays(arrays, names, axes)
4262 blocks = form_blocks(arrays, names, axes)
-> 4263 mgr = BlockManager(blocks, axes)
4264 mgr._consolidate_inplace()
/opt/conda/lib/python3.6/site-packages/pandas/core/internals.py in
__init__(self, blocks, axes, do_integrity_check, fastpath)
2760 if do_integrity_check:
-> 2761 self._verify_integrity()
2762
/opt/conda/lib/python3.6/site-packages/pandas/core/internals.py in
_verify_integrity(self)
2970 if block._verify_integrity and block.shape[1:] !=
mgr_shape[1:]:
-> 2971 construction_error(tot_items, block.shape[1:],
self.axes)
2972 if len(self.items) != tot_items:
/opt/conda/lib/python3.6/site-packages/pandas/core/internals.py in
construction_error(tot_items, block_shape, axes, e)
4232 raise ValueError("Shape of passed values is {0}, indices imply
{1}".format(
-> 4233 passed, implied))
4234
ValueError: Shape of passed values is (2000, 256), indices imply (2000, 5)
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-15-c473d99817c3> in <module>()
4 return compressed_image.ravel()
5
----> 6 cloud_feat_nir = cloud_df_samp.apply(feat_ext_nir, axis=1)
7 cloud_feat_nir.describe()
/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in apply(self,
func,
axis, broadcast, raw, reduce, args, **kwds)
【问题讨论】:
-
'image_name' 列的 dtype 是什么?
cloud_df_samp['image_name'].dtype。另外,您是否需要 feat_ext_nir() 函数将数据帧作为输入和输出?将“image_name”列导出到 numpy.ndarray 以完成转换是否足够?cloud_df_samp['image_name'].values -
'image_name' 列的 dtype 是 object(它是一个字符串)。我试图使用数据帧作为输入和输出的原因是我试图保留索引。我最终想将输出数据框与原始数据框连接起来。我可能会用 ndarrays 破解一些东西,但我会寻求最佳实践。
-
话虽如此,使用 ndarrays 似乎也并不简单。似乎没有一种干净的方法来获取一维数组并应用函数来获取二维数组。
标签: python-3.x pandas