首页 > 解决方案 > 如何使用包含列表中值的字典扩展 pydatable 的列?

问题描述

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

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 步:

使用上述 dict 创建的新数据表并附加到数据表 DT_EX 为,

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 步案例?

你能写下你的评论/答案吗?

标签: pythonpy-datatable

解决方案


您可以将字典包装在 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[]调用评估字典时,我们将其简单地视为元素列表,其中列表中的每个项目都由相应的键命名。如果一个“项目”产生多个列,那么每个列都从键中获得相同的名称。现在,在这种情况下,每个“项目”又是一个列表,我们没有任何特殊规则来评估这些原语列表。所以他们最终会扩展成一个列列表,其中每列都是一个常数。

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


推荐阅读