如何使用包含列表中值的字典扩展 pydatable 的列?

How to extend the columns of a pydatable with a dictionary containing the values in list?

我创建了一个示例数据表,

DT_EX = dt.Frame({'recency': ['current','savings','fixex','current','savings','fixed','savings','current'],
                  'amount': [4200,2300,1500,8000,1200,6500,4500,9010],
                  'no_of_pl': [3,2,1,5,1,2,5,4],
                  'default': [True,False,True,False,True,True,True,False]})

可以看作,

   | recency  amount  no_of_pl  default
-- + -------  ------  --------  -------
 0 | current    4200         3        1
 1 | savings    2300         2        0
 2 | fixex      1500         1        1
 3 | current    8000         5        0
 4 | savings    1200         1        1
 5 | fixed      6500         2        1
 6 | savings    4500         5        1
 7 | current    9010         4        0

[8 rows x 4 columns]

我正在按照以下步骤进行一些数据操作:

第 1 步:将两个新列添加到数据表中

DT_EX[:, f[:].extend({"total_amount": f.amount*f.no_of_pl,
                      'test_col': f.amount/f.no_of_pl})]

输出:

   | recency  amount  no_of_pl  default  total_amount  test_col
-- + -------  ------  --------  -------  ------------  --------
 0 | current    4200         3        1         12600    1400  
 1 | savings    2300         2        0          4600    1150  
 2 | fixex      1500         1        1          1500    1500  
 3 | current    8000         5        0         40000    1600  
 4 | savings    1200         1        1          1200    1200  
 5 | fixed      6500         2        1         13000    3250  
 6 | savings    4500         5        1         22500     900  
 7 | current    9010         4        0         36040    2252.5

[8 rows x 6 columns]

第 2 步:

字典创建为,注意它的值存储在列表

test_dict = {'discount': [10,20,30,40,50,60,70,80],
             'charges': [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8]}

第 3 步:

使用上述字典创建的新数据表并附加到数据表 DT_EX as,

dt.cbind(DT_EX, dt.Frame(test_dict))

输出:

   | recency  amount  no_of_pl  default  discount  charges
-- + -------  ------  --------  -------  --------  -------
 0 | current    4200         3        1        10      0.1
 1 | savings    2300         2        0        20      0.2
 2 | fixex      1500         1        1        30      0.3
 3 | current    8000         5        0        40      0.4
 4 | savings    1200         1        1        50      0.5
 5 | fixed      6500         2        1        60      0.6
 6 | savings    4500         5        1        70      0.7
 7 | current    9010         4        0        80      0.8

[8 rows x 6 columns]

在这里我们可以看到一个包含新添加的列(折扣、费用)的数据表

第 4 步:

正如我们所知,扩展函数可用于添加我试图在名为 test_dict 的字典中传递的列,

DT_EX[:, f[:].extend(test_dict)]

输出:

Out[18]: 
   | recency  amount  no_of_pl  default  discount  discount.0  discount.1  discount.2  discount.3  discount.4  …  charges.2  charges.3  charges.4  charges.5  charges.6
-- + -------  ------  --------  -------  --------  ----------  ----------  ----------  ----------  ----------     ---------  ---------  ---------  ---------  ---------
 0 | current    4200         3        1        10          20          30          40          50          60  …        0.4        0.5        0.6        0.7        0.8
 1 | savings    2300         2        0        10          20          30          40          50          60  …        0.4        0.5        0.6        0.7        0.8
 2 | fixex      1500         1        1        10          20          30          40          50          60  …        0.4        0.5        0.6        0.7        0.8
 3 | current    8000         5        0        10          20          30          40          50          60  …        0.4        0.5        0.6        0.7        0.8
 4 | savings    1200         1        1        10          20          30          40          50          60  …        0.4        0.5        0.6        0.7        0.8
 5 | fixed      6500         2        1        10          20          30          40          50          60  …        0.4        0.5        0.6        0.7        0.8
 6 | savings    4500         5        1        10          20          30          40          50          60  …        0.4        0.5        0.6        0.7        0.8
 7 | current    9010         4        0        10          20          30          40          50          60  …        0.4        0.5        0.6        0.7        0.8

[8 rows x 20 columns]

注意:在输出中可以看到,为每个字典键(折扣、费用)创建了大约 8 列(填写了列表的每个元素) ) 和新添加的列总数为 16.

第 5 步:

我想过用 numpy 数组的值创建一个字典,

test_dict_1 = {'discount': np.array([10,20,30,40,50,60,70,80]),
               'charges': np.array([0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8])}

我已经通过 test_dict_1 将函数扩展为

DT_EX[:, f[:].extend(test_dict_1)]

输出:

Out[20]: 
   | recency  amount  no_of_pl  default  discount  charges
-- + -------  ------  --------  -------  --------  -------
 0 | current    4200         3        1        10      0.1
 1 | savings    2300         2        0        20      0.2
 2 | fixex      1500         1        1        30      0.3
 3 | current    8000         5        0        40      0.4
 4 | savings    1200         1        1        50      0.5
 5 | fixed      6500         2        1        60      0.6
 6 | savings    4500         5        1        70      0.7
 7 | current    9010         4        0        80      0.8

[8 rows x 6 columns]

在这一步,extend 获取了字典并将新列添加到 DT_EX。这是预期的输出。

所以,在这里我想了解一下第4步发生了什么?为什么不从字典键中获取值列表来添加新列?为什么执行第5步的案例?

可以请你在上面写下你的comments/answers吗?

您可以将字典包装在 Frame 构造函数中以获得所需的结果:

>>> DT_EX[:, f[:].extend(dt.Frame(test_dict))]
   | recency  amount  no_of_pl  default  discount  charges
-- + -------  ------  --------  -------  --------  -------
 0 | current    4200         3        1        10      0.1
 1 | savings    2300         2        0        20      0.2
 2 | fixex      1500         1        1        30      0.3
 3 | current    8000         5        0        40      0.4
 4 | savings    1200         1        1        50      0.5
 5 | fixed      6500         2        1        60      0.6
 6 | savings    4500         5        1        70      0.7
 7 | current    9010         4        0        80      0.8

[8 rows x 6 columns]

至于第 4 步中发生的事情,应用了以下逻辑:当我们为 DT[] 调用评估字典时,我们将其简单地视为一个元素列表,其中列表中的每个项目都是由相应的键命名。如果 "item" 产生多列,则每一列都从键中获得相同的名称。现在,在这种情况下,每个 "item" 又是一个列表,我们没有任何特殊规则来评估此类基元列表。所以他们最终扩展成一个列列表,其中每一列都是一个常数。

你是对的,最终结果看起来很违反直觉,所以我们可能想要调整在 DT[] 表达式中评估列表的规则。