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.
For most data types, pandas uses NumPy arrays as the concrete objects contained with a Index , Series , or 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.
You can insert a list of values into a cell in Pandas DataFrame using DataFrame.at() , DataFrame. iat() , and DataFrame. loc() methods.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With