pandas series repeat n time and change column value

前端 未结 4 1367
半阙折子戏
半阙折子戏 2021-01-13 03:14

I have input data like this.

NAME | PLACE | DATE
  A  |   X   | 2020-04-30
  B  |   Y   | 2019-04-30

i want to duplicate 5 times and change d

4条回答
  •  天涯浪人
    2021-01-13 03:41

    Here is a way to do it:

    df_out = df.reindex(df.index.repeat(6))
    
    df_out['DATE'] += pd.Series([pd.DateOffset(years=i) 
                                  for i in df_out.groupby('AME').cumcount()], 
                                index=df_out.index)    
    df_out.reset_index(drop=True)
    

    Output:

          AME    PLACE       DATE
    0     A       X    2020-04-30
    1     A       X    2021-04-30
    2     A       X    2022-04-30
    3     A       X    2023-04-30
    4     A       X    2024-04-30
    5     A       X    2025-04-30
    6     B       Y    2019-04-30
    7     B       Y    2020-04-30
    8     B       Y    2021-04-30
    9     B       Y    2022-04-30
    10    B       Y    2023-04-30
    11    B       Y    2024-04-30
    

提交回复
热议问题