我有以下 DF
。
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
可能看起来像这样。
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 |
需要:
s=df.day_count
s1=[pd.Timedelta(x,'D') for x in sum(df.day_count.apply(lambda x : list(range(x))),[])]
df_new=df.reindex(df.index.repeat(s))
df_new['hold_date']=df_new.hold_date+s1
df_new
Out[642]:
hold_date day_count qty item ccy
0 2015-01-01 1 1200 CB04box USD
1 2015-01-01 3 1500 AB01box USD
1 2015-01-02 3 1500 AB01box USD
1 2015-01-03 3 1500 AB01box USD
2 2015-01-02 2 550 CB03box USD
2 2015-01-03 2 550 CB03box USD
我是一名优秀的程序员,十分优秀!