Python,如何将“线性”数据集格式重塑为类似矩阵的数据集形式

2024-06-16 11:18:05 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个如下格式的数据集

                    Date       Value   
AAAA_Property1   2015/07/22     0.01      
AAAA_Property1   2015/07/23     0.02
.
.
.
AAAA_Property2   2015/07/22     0.88
AAAA_Property2   2015/07/23     0.80
.
.
.
.
BBBB_Property1   2015/07/22     0.04      
BBBB_Property1   2015/07/23     0.07
.
.
.
BBBB_Property2   2015/07/22     0.72
BBBB_Property2   2015/07/23     0.70
.
.
.
.

如您所见,每个AAAA、BBBB、CCCC都有许多属性(property1、property2等),它们的分布非常广泛。(所以它实际上是一个“立方”数据集)

现在我想知道如何将事物组合在一起,并使数据集如下所示

Name        Date        Property1    Property2   . . . 
AAAA     2015/07/22       0.01          0.3   
AAAA     2015/07/23       0.02          0.4   
.
.
.
BBBB     2015/07/22       0.02          0.4   
BBBB     2015/07/23       0.09          0.7   
.
.
.

我需要对numpy及其ndarray进行一些研究,并且知道可以使用reformate()方法和where()方法来实现这个目标。但是我不能把它们放在一起,因为这里的数据是三维的。你知道吗

然而,我确实发现了一些与我正在努力实现的目标非常相似的东西。请看一下以下数据

 data = [
        ['Sulfate', 'Nitrate', 'EC', 'OC1', 'OC2', 'OC3', 'OP', 'CO', 'O3'],
        ('Basecase', [
            [0.88, 0.01, 0.03, 0.03, 0.00, 0.06, 0.01, 0.00, 0.00],
            [0.07, 0.95, 0.04, 0.05, 0.00, 0.02, 0.01, 0.00, 0.00],
            [0.01, 0.02, 0.85, 0.19, 0.05, 0.10, 0.00, 0.00, 0.00],
            [0.02, 0.01, 0.07, 0.01, 0.21, 0.12, 0.98, 0.00, 0.00],
            [0.01, 0.01, 0.02, 0.71, 0.74, 0.70, 0.00, 0.00, 0.00]]),
        ('With CO', [
            [0.88, 0.02, 0.02, 0.02, 0.00, 0.05, 0.00, 0.05, 0.00],
            [0.08, 0.94, 0.04, 0.02, 0.00, 0.01, 0.12, 0.04, 0.00],
            [0.01, 0.01, 0.79, 0.10, 0.00, 0.05, 0.00, 0.31, 0.00],
            [0.00, 0.02, 0.03, 0.38, 0.31, 0.31, 0.00, 0.59, 0.00],
            [0.02, 0.02, 0.11, 0.47, 0.69, 0.58, 0.88, 0.00, 0.00]]),
        ('With O3', [
            [0.89, 0.01, 0.07, 0.00, 0.00, 0.05, 0.00, 0.00, 0.03],
            [0.07, 0.95, 0.05, 0.04, 0.00, 0.02, 0.12, 0.00, 0.00],
            [0.01, 0.02, 0.86, 0.27, 0.16, 0.19, 0.00, 0.00, 0.00],
            [0.01, 0.03, 0.00, 0.32, 0.29, 0.27, 0.00, 0.00, 0.95],
            [0.02, 0.00, 0.03, 0.37, 0.56, 0.47, 0.87, 0.00, 0.00]]),
        ('CO & O3', [
            [0.87, 0.01, 0.08, 0.00, 0.00, 0.04, 0.00, 0.00, 0.01],
            [0.09, 0.95, 0.02, 0.03, 0.00, 0.01, 0.13, 0.06, 0.00],
            [0.01, 0.02, 0.71, 0.24, 0.13, 0.16, 0.00, 0.50, 0.00],
            [0.01, 0.03, 0.00, 0.28, 0.24, 0.23, 0.00, 0.44, 0.88],
            [0.02, 0.00, 0.18, 0.45, 0.64, 0.55, 0.86, 0.00, 0.16]])
    ]

我做了一个帮助(数据),结果它是一个列表对象。但是我在(1)如何将我的数据转换成上述格式,以及(2)如何从上述格式中生成额外的子集矩阵,并从中执行矩阵操作上遇到了困难。你知道吗

我想出的一些模拟代码。。。你知道吗

import pandas as pd
import numpy as np
df = pd.read_excel('xxx.xlsx', sheetname='1')
myMatrix = df.as_matrix()
myMatrix = myMatrix[:,:].astype(float)
# parse the first column and separate the AAAAs from the Property's
...
for current in ['AAAA','BBBB', ...]:
       mySubMatrix = myMatrix.where(firstColumn == current)
       mySubMatrix = mySubMatrix.reshape(numberOfDates, numberOfProperties)
       #then append mySubMatrix to the new target matrix

我不希望这个代码运行。。。但这就是我能想到的算法


Tags: the数据方法numpydateas格式co