Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3中为每个组创建日期序列_Python_Python 3.x_Pandas_Date_Seq - Fatal编程技术网

在Python3中为每个组创建日期序列

在Python3中为每个组创建日期序列,python,python-3.x,pandas,date,seq,Python,Python 3.x,Pandas,Date,Seq,我有一个由多个商店/零售商组成的数据集: RETAILER VOLUME DISP PRICE store1 12 15 10 store1 10 8 17 store1 12 13 12 ... store2 22 22 30 store2 17 14 22 store2 23 18 18 ... s

我有一个由多个商店/零售商组成的数据集:

RETAILER    VOLUME  DISP    PRICE
store1      12      15      10
store1      10      8       17
store1      12      13      12
...
store2      22      22      30
store2      17      14      22
store2      23      18      18
...
store3      11      13      10
store3      12      13      13
store3      14      12      11
不幸的是,这个数据集不包含日期,所以我需要为这个表中的每个存储生成日期序列。频率并不是很重要-几天或几个月就可以了

为了方便起见,我们假设它从2000-01-01开始,每个零售商的日期数取决于该零售商的观察次数

我可以在R中处理这个问题:

df <- df %>% 
  arrange(RETAILER) %>% 
  group_by(RETAILER) %>%  
  mutate(dates = seq(as.Date("2000-01-01"), by = "month", length.out = n()))

让我们对
零售商
上的数据框
进行分组
,并使用
cumcount
为每个
零售商创建顺序计数器
,然后
将该计数器映射到
MonthBegin
偏移量,并添加
时间戳('2000-01-01')



让我们对
零售商
上的数据框
进行分组
,并使用
cumcount
为每个
零售商创建顺序计数器
,然后
将该计数器映射到
MonthBegin
偏移量,并添加
时间戳('2000-01-01')



非常感谢。这确实奏效了。从R到Python的转换似乎不像我想象的那么容易@一旦你熟悉了python世界,它将变得更加容易,而且它将变得更加有趣;)非常感谢。这确实奏效了。从R到Python的转换似乎不像我想象的那么容易@一旦你熟悉了python世界,它将变得更加容易,而且它将变得更加有趣;)
RETAILER    VOLUME  DISP    PRICE   DATE
store1      12      15      10      2000-01-01
store1      10      8       17      2000-02-01
store1      12      13      12      2000-03-01
...
store2      22      22      30      2000-01-01
store2      17      14      22      2000-02-01
store2      23      18      18      2000-03-01
...
store3      11      13      10      2000-01-01
store3      12      13      13      2000-02-01
store3      14      12      11      2000-03-01
c = df.groupby('RETAILER').cumcount()
df['DATE'] = pd.Timestamp('2000-01-01') + c.map(pd.offsets.MonthBegin)
  RETAILER  VOLUME  DISP  PRICE       DATE
0   store1      12    15     10 2000-01-01
1   store1      10     8     17 2000-02-01
2   store1      12    13     12 2000-03-01
3   store2      22    22     30 2000-01-01
4   store2      17    14     22 2000-02-01
5   store2      23    18     18 2000-03-01
6   store3      11    13     10 2000-01-01
7   store3      12    13     13 2000-02-01
8   store3      14    12     11 2000-03-01