Store numpy.array in Pandas.DataFrame cells

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), axis=1) 

but it seems that pandas trying to "unpack" numpy.array.

Is there a workaround? Besides using a wrapper (see below)?

I tried reduce=False without success.

EDIT

This works, but I have to use the 'dummy' Data class to wrap the array, which is unsatisfactory and not very elegant.

 class Data: def __init__(self, v): self.v = v meas = pd.read_excel(DATA_FILE) meas['DATA'] = meas.apply( lambda r: Data(np.array(pd.read_csv(r['filename'])))), axis=1 ) 
+16
python numpy pandas dataframe
source share
3 answers

Use a wrapper around the numpy array, i.e. pass the numpy array as a list

 a = np.array([5, 6, 7, 8]) df = pd.DataFrame({"a": [a]}) 

Output:

  a
 0 [5, 6, 7, 8]

Or you can use apply(np.array) by creating tuples, i.e. if you have a dataframe

 df = pd.DataFrame({'id': [1, 2, 3, 4], 'a': ['on', 'on', 'off', 'off'], 'b': ['on', 'off', 'on', 'off']}) df['new'] = df.apply(lambda r: tuple(r), axis=1).apply(np.array) 

Exit:

  ab id new
 0 on on 1 [on, on, 1]
 1 on off 2 [on, off, 2]
 2 off on 3 [off, on, 3]
 3 off off 4 [off, off, 4]
 df['new'][0] 

Exit:

 array(['on', 'on', '1'], dtype='<U2') 
+27
source share

You can wrap the arguments of the Data Frame data in square brackets to support np.array in each cell:

 one_d_array = np.array([1,2,3]) two_d_array = one_d_array*one_d_array[:,np.newaxis] two_d_array array([[1, 2, 3], [2, 4, 6], [3, 6, 9]]) pd.DataFrame([ [one_d_array], [two_d_array] ]) 0 0 [1, 2, 3] 1 [[1, 2, 3], [2, 4, 6], [3, 6, 9]] 
+5
source share

Suppose you have a DataFrame ds and it has a column called 'class'. If ds ['class'] contains strings or numbers, and you want to change them using numpy.ndarray or list s, the following code will help. In the code, class2vector is 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)

+2
source share

All Articles