Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
91 views
in Technique[技术] by (71.8m points)

python - Store 3d-array in a pandas dataframe column

I'd like to store a 3D-numpy array in a column of a dataframe.

df = pd.DataFrame({"nodes": list(range(1, 4))})
df = df.set_index("nodes")

df[0] = list(range(1, 6, 2))
df[1] = [10,20,30]

>>> df
            0       1
nodes              
1           1      10
2           3      20
3           5      30

Example for numpy-array:

test = np.array([[[1,2,3],
                  [4,5,6]],
                 [[10,20,30],
                  [40,50,60]],
                 [[0,1,0],
                  [-1,-1,-1]]])

What i would like to have is:

>>> df

        0        1                          2
nodes
1       1       10          [[1,2,3],[4,5,6]]
2       3       20    [[10,20,30],[40,50,60]]
3       5       30       [[0,1,0],[-1,-1,-1]]

How can i add the 3d-array to a column of the existing dataframe?

question from:https://stackoverflow.com/questions/65848040/store-3d-array-in-a-pandas-dataframe-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use this:

df[2] = test.tolist()

output:

    0   1   2
nodes           
1   1   10  [[1, 2, 3], [4, 5, 6]]
2   3   20  [[10, 20, 30], [40, 50, 60]]
3   5   30  [[0, 1, 0], [-1, -1, -1]]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...