我有以下数据框:

id date_start date_end reporting_month reporting_month_number months_length
1 2022-03-31 23:56:22 2022-05-01 23:56:22 2022-03 1 3
2 2022-03-31 23:48:48 2022-06-01 23:48:48 2022-03 1 4
3 2022-03-31 23:47:36 2022-08-01 23:47:36 2022-03 1 6

我想要拆分每个id行,这样从日期reporting_month开始,我就可以为months_length个ID行中的每一个分配一行,如下所示:

id date_start date_end reporting_month reporting_month_number months_length
1 2022-03-31 23:56:22 2022-05-01 23:56:22 2022-03 1 3
1 2022-03-31 23:56:22 2022-05-01 23:56:22 2022-04 2 3
1 2022-03-31 23:56:22 2022-05-01 23:56:22 2022-05 3 3
2 2022-03-31 23:48:48 2022-06-01 23:48:48 2022-03 1 4
2 2022-03-31 23:48:48 2022-06-01 23:48:48 2022-03 2 4
2 2022-03-31 23:48:48 2022-06-01 23:48:48 2022-04 3 4
2 2022-03-31 23:48:48 2022-06-01 23:48:48 2022-05 4 4
3 2022-03-31 23:47:36 2022-08-01 23:47:36 2022-03 1 6
3 2022-03-31 23:47:36 2022-08-01 23:47:36 2022-04 2 6
3 2022-03-31 23:47:36 2022-08-01 23:47:36 2022-05 3 6
3 2022-03-31 23:47:36 2022-08-01 23:47:36 2022-06 4 6
3 2022-03-31 23:47:36 2022-08-01 23:47:36 2022-07 5 6
3 2022-03-31 23:47:36 2022-08-01 23:47:36 2022-08 6 6

我try 了几种方法,但似乎都达不到我的目标.

有谁对如何实现这一点有什么建议吗?

谢谢.

推荐答案

一种可能的解决方案是,

df= df.loc[df.index.repeat(df['months_length'])].reset_index(drop=True)
df['reporting_month_number'] = df.groupby('id')['reporting_month_number'].cumsum()

运费:运费:

    id           date_start             date_end reporting_month  \
0    1  2022-03-31 23:56:22  2022-05-01 23:56:22         2022-03   
1    1  2022-03-31 23:56:22  2022-05-01 23:56:22         2022-03   
2    1  2022-03-31 23:56:22  2022-05-01 23:56:22         2022-03   
3    2  2022-03-31 23:48:48  2022-06-01 23:48:48         2022-03   
4    2  2022-03-31 23:48:48  2022-06-01 23:48:48         2022-03   
5    2  2022-03-31 23:48:48  2022-06-01 23:48:48         2022-03   
6    2  2022-03-31 23:48:48  2022-06-01 23:48:48         2022-03   
7    3  2022-03-31 23:47:36  2022-08-01 23:47:36         2022-03   
8    3  2022-03-31 23:47:36  2022-08-01 23:47:36         2022-03   
9    3  2022-03-31 23:47:36  2022-08-01 23:47:36         2022-03   
10   3  2022-03-31 23:47:36  2022-08-01 23:47:36         2022-03   
11   3  2022-03-31 23:47:36  2022-08-01 23:47:36         2022-03   
12   3  2022-03-31 23:47:36  2022-08-01 23:47:36         2022-03   

    reporting_month_number  months_length  
0                        1              3  
1                        2              3  
2                        3              3  
3                        1              4  
4                        2              4  
5                        3              4  
6                        4              4  
7                        1              6  
8                        2              6  
9                        3              6  
10                       4              6  
11                       5              6  
12                       6              6  

​ 解释:

  1. 根据MONTS_LENGTH重复行
  2. 根据Groupby‘id’更新报告月份编号

Python相关问答推荐

使用pandas MultiIndex进行不连续 Select

Flask主机持续 bootstrap 本地IP| Python

不允许AMBIMA API请求方法

指示组内的rejected_time是否在creation_timestamp后5分钟内

在上下文管理器中更改异常类型

Pandas 第二小值有条件

2维数组9x9,不使用numpy.数组(MutableSequence的子类)

如果条件为真,则Groupby.mean()

@Property方法上的inspect.getmembers出现意外行为,引发异常

Python json.转储包含一些UTF-8字符的二元组,要么失败,要么转换它们.我希望编码字符按原样保留

将两只Pandas rame乘以指数

无法通过python-jira访问jira工作日志(log)中的 comments

如何在python polars中停止otherate(),当使用when()表达式时?

如何在给定的条件下使numpy数组的计算速度最快?

运输问题分支定界法&

如何在表中添加重复的列?

Python逻辑操作作为Pandas中的条件

python中的解释会在后台调用函数吗?

处理具有多个独立头的CSV文件

在numpy数组中寻找楼梯状 struct