熊猫df可以具有numpy数组的单元格值吗

时间:2019-09-25 11:17:24

标签: pandas numpy

我想将Numpy数组存储为Dataframe中单元的值。有什么办法吗? 基本上我有一个像素数据,它是一个(512,512)Numpy数组,我想将其保存为与dataframe ID列中其特定ID对应的pixel_data列的值。我该怎么办?

这里是我尝试过的:

for f in train_files[:10]:
    id_tmp = f.split('/')[4].split('.')[0]
    first_dcm = pydicom.read_file(f)
    img = first_dcm.pixel_array
    window = get_windowing(first_dcm)
    image = window_image(img, *window)
    train.loc[train.Image == id_tmp, 'img_before_w'] = img
    train.loc[train.Image == id_tmp, 'img_after_w'] = image

我得到的错误:

ValueError                                Traceback (most recent call last)
<ipython-input-47-32236f8c9ccc> in <module>
      5     window = get_windowing(first_dcm)
      6     image = window_image(img, *window)
----> 7     train.loc[train.Image == id_tmp, 'img_before_w'] = img
      8     train.loc[train.Image == id_tmp, 'img_after_w'] = image
      9 

/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py in __setitem__(self, key, value)
    203             key = com.apply_if_callable(key, self.obj)
    204         indexer = self._get_setitem_indexer(key)
--> 205         self._setitem_with_indexer(indexer, value)
    206 
    207     def _validate_key(self, key, axis: int):

/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py in _setitem_with_indexer(self, indexer, value)
    525                     if len(labels) != value.shape[1]:
    526                         raise ValueError(
--> 527                             "Must have equal len keys and value "
    528                             "when setting with an ndarray"
    529                         )

ValueError: Must have equal len keys and value when setting with an ndarray

1 个答案:

答案 0 :(得分:0)

采用以下示例数据框:

train=pd.DataFrame({'Image':[1,2,3,2],'img_before_w':[np.nan, np.nan, np.nan,np.nan]})

print(train)给出

   Image  img_before_w
0      1           NaN
1      2           NaN
2      3           NaN
3      2           NaN

例如,现在,如果您想在train.Image == 2时插入像素数据,则可以使用以下代码来实现:

mask = train.Image == 2 # contains True for desired rows
target_index=mask[mask==True].index # gives index of rows, wherever condition is met
train.loc[mask, 'img_before_w'] = pd.Series([[512,512]]*len(target_index), index=target_index)  # inserts [512,512] array in rows wherever condition is met, in given column

现在,print(train)给出了所需的输出:

Image img_before_w
0      1          NaN
1      2   [512, 512]
2      3          NaN
3      2   [512, 512]