Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a numpy arrays in a column of a Pandas dataframe?

Is it possible to store arbitrary numpy arrays as the values of a single column in a dataframe of Pandas?

The arrays are all 2-dimensional, and I intend to use them to calculate values for other columns in the same dataframe.

To provide some context of what I'm trying to do here:

Each array is an adjacency matrix of some network, and for each network I want to calculate its various characteristics (e.g. density, centralities, clustering coefficient, etc) which are in fact other columns in the same dataframe.

like image 751
MLister Avatar asked Oct 24 '13 18:10

MLister


People also ask

Can pandas DataFrame hold NumPy array?

For most data types, pandas uses NumPy arrays as the concrete objects contained with a Index , Series , or DataFrame .

How do you add an array to a pandas DataFrame?

To convert an array to a dataframe with Python you need to 1) have your NumPy array (e.g., np_array), and 2) use the pd. DataFrame() constructor like this: df = pd. DataFrame(np_array, columns=['Column1', 'Column2']) . Remember, that each column in your NumPy array needs to be named with columns.

Can you store a list in a DataFrame cell?

You can insert a list of values into a cell in Pandas DataFrame using DataFrame.at() , DataFrame. iat() , and DataFrame. loc() methods.


1 Answers

Store them as elements as you would do for any other data:

import numpy as np
import pandas as pd
a = np.arange(10).reshape(2,5)
b = np.arange(10, 20).reshape(2,5)
pd.DataFrame({'foo':[42,51], 'arr':[a,b]})
Out[10]: 
                                            arr  foo
0            [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]   42
1  [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]   51

Note that what you try to do sounds more to use a Panel.

like image 89
Zeugma Avatar answered Oct 02 '22 19:10

Zeugma