【问题标题】:Create DataArray from Dict of 2D DataFrames/Arrays从 2D DataFrames/Arrays 的字典创建 DataArray
【发布时间】:2016-08-25 05:07:15
【问题描述】:

我正在尝试从Pandas 转换为Xarray for N-Dimensional DataArrays 以扩展我的曲目。

实际上,我将有一堆不同的pd.DataFrames(在本例中为 row=month,col=attribute)沿着我想合并的特定轴(下面的模拟示例中的患者)(w /o 使用面板或多索引 :),谢谢)。我想将它们转换为xr.DataArrays,以便我可以在它们之上构建尺寸。我制作了一个模拟数据集来说明我在说什么。

对于我制作的这个数据集,想象一下100 patients, 12 months, 10000 attributes, 3 replicates (per attribute),这将是一个典型的 4D 数据集。基本上,我将3 replicates per attribute 压缩为mean 所以我最终得到一个二维pd.DataFrame(行=月,列=属性)这个DataFrame是我字典中的值,它来自的患者是关键(即(患者_x:DataFrame_X))

我还将介绍一下我使用np.ndarray 占位符的方法,但是如果我可以从键为 patient_x 和值的字典中生成 N 维 DataArray,那将非常方便是一个 DataFrame_X

如何使用 XarrayPandas DataFrames 的字典中创建 N 维 DataArray

import xarray as xr
import numpy as np
import pandas as pd

np.random.seed(1618033)

#Set dimensions
a,b,c,d = 100,12,10000,3 #100 patients, 12 months, 10000 attributes, 3 replicates

#Create labels
patients = ["patient_%d" % i for i in range(a)]
months = [j for j in range(b)]
attributes = ["attr_%d" % k for k in range(c)]
replicates = [l for l in range(d)]

coords = [patients,months,attributes]
dims = ["Patients","Months","Attributes"]

#Dict of DataFrames
D_patient_DF = dict()

for i, patient in enumerate(patients):
    A_placeholder = np.zeros((b,c))
    for j, month in enumerate(months):
        #Attribute x Replicates
        A_attrReplicates = np.random.random((c,d))
        #Collapse into 1D Vector
        V_attrExp = A_attrReplicates.mean(axis=1)
        #Fill array with row
        A_placeholder[j,:] = V_attrExp
    #Assign dataframe for every patient
    DF_data = pd.DataFrame(A_placeholder, index = months, columns = attributes)
    D_patient_DF[patient] = DF_data

 xr.DataArray(D_patient_DF).dims
#() its empty

D_patient_DF
#{'patient_0':       attr_0    attr_1    attr_2    attr_3    attr_4    attr_5    attr_6  \
# 0   0.445446  0.422018  0.343454  0.140700  0.567435  0.362194  0.563799   
# 1   0.440010  0.548535  0.810903  0.482867  0.469542  0.591939  0.579344   
# 2   0.645719  0.450773  0.386939  0.418496  0.508290  0.431033  0.622270   
# 3   0.555855  0.633393  0.555197  0.556342  0.489865  0.204200  0.823043   
# 4   0.916768  0.590534  0.597989  0.592359  0.484624  0.478347  0.507789   
# 5   0.847069  0.634923  0.591008  0.249107  0.655182  0.394640  0.579700   
# 6   0.700385  0.505331  0.377745  0.651936  0.334216  0.489728  0.282544   
# 7   0.777810  0.423889  0.414316  0.389318  0.565144  0.394320  0.511034   
# 8   0.440633  0.069643  0.675037  0.365963  0.647660  0.520047  0.539253   
# 9   0.333213  0.328315  0.662203  0.594030  0.790758  0.754032  0.602375   
# 10  0.470330  0.419496  0.171292  0.677439  0.683759  0.646363  0.465788   
# 11  0.758556  0.674664  0.801860  0.612087  0.567770  0.801514  0.179939   

【问题讨论】:

    标签: python dictionary pandas dataframe python-xarray


    【解决方案1】:

    从 DataFrames 字典中,您可以将每个值转换为 DataArray(添加维度标签),将结果加载到 Dataset 中,然后转换为 DataArray:

    variables = {k: xr.DataArray(v, dims=['month', 'attribute'])
                 for k, v in D_patient_DF.items()}
    combined = xr.Dataset(variables).to_array(dim='patient')
    print(combined)
    

    但是,请注意,结果不一定按排序顺序排序,而是使用字典迭代的任意顺序。如果你想要排序的顺序,你应该使用 OrderedDict 代替(在上面设置variables 之后插入):

    variables = collections.OrderedDict((k, variables[k]) for k in patients)
    

    这个输出:

    <xarray.DataArray (patient: 100, month: 12, attribute: 10000)>
    array([[[ 0.61176399,  0.26172557,  0.74657302, ...,  0.43742111,
              0.47503291,  0.37263983],
            [ 0.34970732,  0.81527751,  0.53612895, ...,  0.68971198,
              0.68962168,  0.75103198],
            [ 0.71282751,  0.23143891,  0.28481889, ...,  0.52612376,
              0.56992843,  0.3483683 ],
            ...,
            [ 0.84627257,  0.5033482 ,  0.44116194, ...,  0.55020168,
              0.48151353,  0.36374339],
            [ 0.53336826,  0.59566147,  0.45269417, ...,  0.41951078,
              0.46815364,  0.44630235],
            [ 0.25720899,  0.18738289,  0.66639783, ...,  0.36149276,
              0.58865823,  0.33918553]],
    
           ...,
    
           [[ 0.42933273,  0.58642504,  0.38716496, ...,  0.45667285,
              0.72684589,  0.52335464],
            [ 0.34946576,  0.35821339,  0.33097093, ...,  0.59037927,
              0.30233665,  0.6515749 ],
            [ 0.63673498,  0.31022272,  0.65788374, ...,  0.47881873,
              0.67825066,  0.58704331],
            ...,
            [ 0.44822441,  0.502429  ,  0.50677081, ...,  0.4843405 ,
              0.84396521,  0.45460029],
            [ 0.61336348,  0.46338301,  0.60715273, ...,  0.48322379,
              0.66530209,  0.52204897],
            [ 0.47520639,  0.43490559,  0.27309414, ...,  0.35280585,
              0.30280485,  0.77537204]]])
    Coordinates:
      * month      (month) int64 0 1 2 3 4 5 6 7 8 9 10 11
      * patient    (patient) <U10 'patient_80' 'patient_73' 'patient_79' ...
      * attribute  (attribute) object 'attr_0' 'attr_1' 'attr_2' 'attr_3' ...
    

    或者,您可以创建一个二维数据数组列表,然后使用concat

    patient_list = []
    for i, patient in enumerate(patients):
        df = ...
        array = xr.DataArray(df, dims=['patient', 'attribute'])
        patient_list.append(df)
    combined = xr.concat(patient_list, dim=pd.Index(patients, name='patient')
    

    这将给出相同的结果,并且可能是最干净的代码。

    【讨论】:

    • 嘿@Stephan 感谢您的回复。在您写的第一部分中,我尝试了 variables = {(k, xr.DataArray(v, dims=['month', 'attribute'])) for k, v in list(D_patient_DF.items())} 并收到以下错误:TypeError: unhashable type: 'DataArray'。我使用的是 Python 3.5,所以我将 D_patient_DF.items() 更改为 list(D_patient_DF.items())
    • 我喜欢你的最后一个例子。我最终对其进行了调整以绕过数据框,然后直接转到 dataarray D_patient_DA[patient] = xr.DataArray(A_placeholder, coords = [months, attributes], dims = ["Months","Attributes"]) 然后我做 DA_data = xr.concat(list(D_patient_DA.values()), dim="Patients") 但我无法将标签分配给患者(或 coords)。
    • @O.rka 很好,我在编辑代码时犯了一个错误——我在第一个示例中修复了字典理解。
    • 非常感谢@Stephan。我不知道有人可以在一个新维度上与 2D DataArrays 连接!
    猜你喜欢
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2014-10-02
    • 2016-11-20
    相关资源
    最近更新 更多