Python:堆栈和枚举日期以创建新记录

2024-06-23 19:57:28 发布

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

我目前有一个数据帧,格式如下:

id,transaction_dt,units,measure
1,2014-01-06,30,30.5
1,2014-02-04,5,22.6

我希望达到以下目标:

创建新记录的基础是将单位按id添加到事务中,以创建如下所示的结果数据框:

id,enumerated_dt,measure
1,2014-01-06,30.5
1,2014-01-07,30.5
1,2014-01-08,30.5
...
1,2014-02-05,30.5
1,2014-02-04,22.6
1,2014-02-05,22.6
...

我认为stack可能是解决方案的一部分,但我目前正被困在如何以一种高效的方式枚举/增加日期上。任何建议或解决方案将不胜感激!你知道吗


Tags: 数据id目标stack格式记录dt单位
2条回答

编辑:在第二眼看来,您确实希望填补日期的空白,然后向前填充数据,这可以很容易地完成:

df.set_index('transaction_dt').resample('D').ffill()

但是,如果您不想创建连续索引,而是想添加任意数量的行N,您可以先将“transaction\u dt”移动到索引:

df.set_index('transaction_dt', inplace=True)

…然后使用这个lambda函数(使用numpy方法):

add_n_dates = lambda n: df.index.repeat(n) + \
                        np.tile(np.arange(n)*pd.Timedelta('1 days'), df.index.size)

。。。在最终重新索引+向前填充之前,要向新索引的每个元素添加n日期:

df.reindex(add_n_dates(5), method='ffill')

#                  id  units  measure
# transaction_dt                     
# 2014-01-06      1.0   30.0     30.5
# 2014-01-07      1.0   30.0     30.5
# 2014-01-08      1.0   30.0     30.5
# 2014-01-09      1.0   30.0     30.5
# 2014-01-10      1.0   30.0     30.5
# 2014-02-04      1.0    5.0     22.6
# 2014-02-05      1.0    5.0     22.6
# 2014-02-06      1.0    5.0     22.6
# 2014-02-07      1.0    5.0     22.6
# 2014-02-08      1.0    5.0     22.6

编辑#2:

再次假设您已经将索引设置为transaction_dt,这可能是使用units中的值来确定要添加多少行的最简单方法。它使用pd.date_range通过传递row.name(即它的索引值)作为起始点,row.units作为要生成的时段来创建必要的日期值。你知道吗

df.apply(lambda x: pd.Series(pd.date_range(x.name, periods=x.units)), axis=1). \
    stack(). \
    reset_index(level=1). \
    join(df['measure']). \
    drop('level_1', axis=1). \
    reset_index(). \
    rename(columns={0:'enumerated_dt'})

#    transaction_dt enumerated_dt  measure
# 0      2014-01-06    2014-01-06     30.5
# 1      2014-01-06    2014-01-07     30.5
# 2      2014-01-06    2014-01-08     30.5
# 3      2014-01-06    2014-01-09     30.5
# 4      2014-01-06    2014-01-10     30.5
# ...
# 29     2014-01-06    2014-02-04     30.5
# 30     2014-02-04    2014-02-04     22.6
# 31     2014-02-04    2014-02-05     22.6
# 32     2014-02-04    2014-02-06     22.6
# 33     2014-02-04    2014-02-07     22.6
# 34     2014-02-04    2014-02-08     22.6

您可以创建帮助数据框

df=pd.DataFrame({'transaction_dt':pd.date_range('2014-01-06',periods = 35,freq='D')})

df.merge(df1,how='left').ffill()
Out[879]: 
   transaction_dt   id  units  measure
0      2014-01-06  1.0   30.0     30.5
1      2014-01-07  1.0   30.0     30.5
2      2014-01-08  1.0   30.0     30.5
3      2014-01-09  1.0   30.0     30.5
4      2014-01-10  1.0   30.0     30.5
5      2014-01-11  1.0   30.0     30.5
6      2014-01-12  1.0   30.0     30.5
7      2014-01-13  1.0   30.0     30.5
8      2014-01-14  1.0   30.0     30.5
9      2014-01-15  1.0   30.0     30.5
10     2014-01-16  1.0   30.0     30.5
11     2014-01-17  1.0   30.0     30.5
12     2014-01-18  1.0   30.0     30.5
13     2014-01-19  1.0   30.0     30.5
14     2014-01-20  1.0   30.0     30.5
15     2014-01-21  1.0   30.0     30.5
16     2014-01-22  1.0   30.0     30.5
17     2014-01-23  1.0   30.0     30.5
18     2014-01-24  1.0   30.0     30.5
19     2014-01-25  1.0   30.0     30.5
20     2014-01-26  1.0   30.0     30.5
21     2014-01-27  1.0   30.0     30.5
22     2014-01-28  1.0   30.0     30.5
23     2014-01-29  1.0   30.0     30.5
24     2014-01-30  1.0   30.0     30.5
25     2014-01-31  1.0   30.0     30.5
26     2014-02-01  1.0   30.0     30.5
27     2014-02-02  1.0   30.0     30.5
28     2014-02-03  1.0   30.0     30.5
29     2014-02-04  1.0    5.0     22.6
30     2014-02-05  1.0    5.0     22.6
31     2014-02-06  1.0    5.0     22.6
32     2014-02-07  1.0    5.0     22.6
33     2014-02-08  1.0    5.0     22.6
34     2014-02-09  1.0    5.0     22.6

相关问题 更多 >

    热门问题