我有下面的DF
。你知道吗
数据框数量
| hold_date | day_count | qty | item | ccy |
+------------+-----------+------+----------+-----+
| 2015-01-01 | 1 | 1200 | CB04 box | USD |
| 2015-01-01 | 3 | 1500 | AB01 box | USD |
| 2015-01-02 | 2 | 550 | CB03 box | USD |
我想在day_count
的基础上增加hold_date
。例如item
:AB01 box
将添加两个新行,如下所示。所以df
看起来像这样。你知道吗
数据框数量
| hold_date | qty | item | ccy |
+------------+------+----------+-----+
| 2015-01-01 | 1200 | CB04 box | USD |
| 2015-01-01 | 1500 | AB01 box | USD |
| 2015-01-02 | 1500 | AB01 box | USD |
| 2015-01-03 | 1500 | AB01 box | USD |
| 2015-01-02 | 550 | CB03 box | USD |
| 2015-01-03 | 550 | CB03 box | USD |
需要:
这是一个完全矢量化(无
for
循环)的解决方案。其思想是创建一个包含所有日期列表的临时列,然后将其展开成行。expand_column
函数基于this answer。你知道吗您可以通过从DF qty创建一个新的
DataFrame
并重复元素qty*次来实现这一点:这将创建一个新列表,其中包含foreach行的
qty
*副本。你知道吗相关问题 更多 >
编程相关推荐