![](/img/trans.png)
[英]How to best coerce a list of numpy arrays into a pandas dataframe column?
[英]How to store a numpy arrays in a column of a Pandas dataframe?
是否可以將任意numpy
數組存儲為Pandas
數據框中單個列的值?
這些數組都是二維的,我打算用它們來計算同一數據幀中其他列的值。
提供我在這里嘗試做的一些背景:
每個陣列都是某個網絡的鄰接矩陣,對於每個網絡,我想計算其各種特征(例如密度,中心,聚類系數等),這些特征實際上是同一數據幀中的其他列。
將它們存儲為元素,就像對待任何其他數據一樣:
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
請注意,您嘗試執行的操作聽起來更多使用Panel
。
你的意思是將任意numpy數組存儲為Pandas數據框中列的值?
像這樣的東西?
import numpy as np
import pandas as pd
x = np.random.randn(50, 25)
random_frame = pd.DataFrame(x)
這會將數組x存儲在DataFrame中,其中列名為0,1,2,3 ...你能澄清一下嗎? 我認為這更像是一個評論,但我不知道我是否可以發表評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.