'传递值的形状是x,索引意味着y'熊猫适用

时间:2017-05-12 21:07:21

标签: python-3.x pandas

我有这段代码:

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)

1 个答案:

答案 0 :(得分:0)

由于我不确定输入数据的细节,但是这段代码是否有效,这是一个黑暗的镜头?:

def feat_ext_nir(x):
    img = loadImage(x['image_name'])
    compressed_img = compress_image(16, 16, img[:,:,3])
    return compressed_image.ravel().tolist()

cloud_feat_nir = pd.Series(cloud_df_samp.apply(feat_ext_nir, axis=1))
cloud_feat_nir.describe()