Store numpy.array in cells of a Pandas.DataFrame

Solution 1:

Use a wrapper around the numpy array i.e. pass the numpy array as 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 the 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)

Output :

     a    b  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]

Output :

array(['on', 'on', '1'], dtype='<U2')

Solution 2:

If you first set a column to have type object, you can insert an array without any wrapping:

df = pd.DataFrame(columns=[1])
df[1] = df[1].astype(object)
df.loc[1, 1] = np.array([5, 6, 7, 8])
df

Output:

    1
1   [5, 6, 7, 8]