我希望能够创建n-dimensional
数据框。我听说过一种使用 3D 数据帧的方法panels
in pandas
但是,如果可能的话,我想通过将不同的数据集组合成一个,将维度扩展到超过 3 个维度超级数据框
我尝试了这个,但我不知道如何在我的测试数据集上使用这些方法 - >构建 3D Pandas 数据框 https://stackoverflow.com/questions/24290495/constructing-3d-pandas-dataframe
另外,这对我的情况没有帮助 ->Pandas Dataframe 或 Panel 到 3d numpy 数组 https://stackoverflow.com/questions/23478297/pandas-dataframe-or-panel-to-3d-numpy-array
我用任意轴数据制作了一个随机测试数据集,试图模仿真实情况;有 3 个轴(即患者、年份和样本)。我尝试将一堆数据框添加到列表中,然后用它制作一个数据框,但它不起作用:(我什至尝试了panel
如上面的第二个链接所示,但我也无法让它工作。
有谁知道如何创建带有标签的 N 维 pandas 数据框?
我尝试的第一种方法:
#Reproducibility
np.random.seed(1618033)
#Set 3 axis labels/dims
axis_1 = np.arange(2000,2010) #Years
axis_2 = np.arange(0,20) #Samples
axis_3 = np.array(["patient_%d" % i for i in range(0,3)]) #Patients
#Create random 3D array to simulate data from dims above
A_3D = np.random.random((years.size, samples.size, len(patients))) #(10, 20, 3)
#Create empty list to store 2D dataframes (axis_2=rows, axis_3=columns) along axis_1
list_of_dataframes=[]
#Iterate through all of the year indices
for i in range(axis_1.size):
#Create dataframe of (samples, patients)
DF_slice = pd.DataFrame(A_3D[i,:,:],index=axis_2,columns=axis_3)
list_of_dataframes.append(DF_slice)
# print(DF_slice) #preview of the 2D dataframes "slice" of the 3D array
# patient_0 patient_1 patient_2
# 0 0.727753 0.154701 0.205916
# 1 0.796355 0.597207 0.897153
# 2 0.603955 0.469707 0.580368
# 3 0.365432 0.852758 0.293725
# 4 0.906906 0.355509 0.994513
# 5 0.576911 0.336848 0.265967
# ...
# 19 0.583495 0.400417 0.020099
# DF_3D = pd.DataFrame(list_of_dataframes,index=axis_2, columns=axis_1)
# Error
# Shape of passed values is (1, 10), indices imply (10, 20)
我尝试的第二种方法:
DF = pd.DataFrame(axis_3,columns=axis_2)
#Error:
#Shape of passed values is (1, 3), indices imply (20, 3)
# p={}
# for i in axis_1:
# p[i]=DF
# panel= pd.Panel(p)
我想我可以做这样的事情,但我真的很喜欢pandas
并且宁愿使用他们的方法之一(如果存在):
#Set data for query
query_year = 2007
query_sample = 15
query_patient = "patient_1"
#Index based on query
A_3D[
(axis_1 == query_year).argmax(),
(axis_2 == query_sample).argmax(),
(axis_3 == query_patient).argmax()
]
#0.1231212416981845
以这种方式访问数据会很棒:
DF_3D[query_year][query_sample][query_patient]
#Where DF_3D[query_year] would give a list of 2D arrays (row=sample, col=patient)
# DF_3D[query_year][query_sample] would give a 1D vector/list of patient data for a particular year, of a particular sample.
# and DF_3D[query_year][query_sample][query_patient] would be a particular sample of a particular patient of a particular year