简体   繁体   English

对于每 1000 行,插入新的递增日期 python pandas

[英]For each 1000 rows, insert new incrementing date python pandas

I have a df like this:我有一个这样的df:

       a     b     c   
0      1     2     3
1      4     5     6
...
1000   7     8     9 

I'm trying to create a column d with an incrementing date for each 1000 rows, like this:我正在尝试为每 1000 行创建一个具有递增日期的列 d,如下所示:

       a     b     c    d
0      1     2     3    4/1/21
1      4     5     6    4/1/21
2      4     5     6    4/1/21
...
1000   7     8     9    4/2/21
1001   7     8     9    4/2/21
...
2000   7     8     9    4/3/21
2001   7     8     9    4/3/21

thinking I should pass in a list of the dates... but really don't know the best way to do this.认为我应该传递日期列表......但真的不知道最好的方法。 just been doing it manually in excel.只是在 excel 中手动进行。

Let's try something like:让我们尝试一下:

import pandas as pd
import numpy as np

# Random Data
df = pd.DataFrame(np.random.randint(1, 10, size=(2002, 3)),
                  columns=['a', 'b', 'c'])

start = pd.to_datetime("4/1/2021")
df['d'] = start + (df.index // 1000 * pd.Timedelta(days=1))
print(df)

Output: Output:

      a  b  c          d
0     7  9  6 2021-04-01
1     3  5  6 2021-04-01
2     7  5  8 2021-04-01
3     8  2  3 2021-04-01
4     6  6  8 2021-04-01
...  .. .. ..        ...
1997  4  3  7 2021-04-02
1998  1  5  2 2021-04-02
1999  1  7  7 2021-04-02
2000  4  8  8 2021-04-03
2001  9  9  6 2021-04-03

[2002 rows x 4 columns]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM