Store numpy.array in cells of a Pandas.DataFrame

前端 未结 5 1900
小鲜肉
小鲜肉 2020-12-02 12:41

I have a dataframe in which I would like to store \'raw\' numpy.array:

df[\'COL_ARRAY\'] = df.apply(lambda r: np.array(do_something_with_r), axi         


        
5条回答
  •  囚心锁ツ
    2020-12-02 13:03

    Suppose you have a DataFrame ds and it has a column named as 'class'. If ds['class'] contains strings or numbers, and you want to change them with numpy.ndarrays or lists, the following code would help. In the code, class2vector is a numpy.ndarray or list and ds_class is a filter condition.

    ds['class'] = ds['class'].map(lambda x: class2vector if (isinstance(x, str) and (x == ds_class)) else x)

提交回复
热议问题