与连续日期添加行

发布于 2025-02-11 07:00:29 字数 851 浏览 3 评论 0 原文

我有此数据框:

ClientID    ArrivalDate         DepartureDate    TotalRevenue
191609   2019-01-15 00:00:00 2019-01-17 00:00:00     5720
213156   2019-01-15 00:00:00 2019-01-16 00:00:00     2130

我想创建一个新列“引用”,值等于“到达”。然后,我想添加一个带有所有相同信息的新行,但“引用”有一天会增加,然后重复此过程,直到“引用”等于“离开”。这应该针对每个客户端进行。最终结果应该看起来像:

ClientID     ArrivalDate         DepartureDate   TotalRevenue    ReferenceDate
191609   2019-01-15 00:00:00 2019-01-17 00:00:00    5720      2019-01-15 00:00:00
191609   2019-01-15 00:00:00 2019-01-17 00:00:00    5720      2019-01-16 00:00:00
191609   2019-01-15 00:00:00 2019-01-17 00:00:00    5720      2019-01-17 00:00:00
213156   2019-01-15 00:00:00 2019-01-16 00:00:00    2130      2019-01-15 00:00:00
213156   2019-01-15 00:00:00 2019-01-16 00:00:00    2130      2019-01-16 00:00:00

有可能吗?

I have this dataframe:

ClientID    ArrivalDate         DepartureDate    TotalRevenue
191609   2019-01-15 00:00:00 2019-01-17 00:00:00     5720
213156   2019-01-15 00:00:00 2019-01-16 00:00:00     2130

And I would like to create a new column, 'ReferenceDate', with value equal to 'ArrivalDate'. Then, I want to add a new row with all same information but 'ReferenceDate' increased one day, and repeat this process until 'ReferenceDate' is equal to 'DepartureDate'. This should be done for each ClientID. Final result should look like this:

ClientID     ArrivalDate         DepartureDate   TotalRevenue    ReferenceDate
191609   2019-01-15 00:00:00 2019-01-17 00:00:00    5720      2019-01-15 00:00:00
191609   2019-01-15 00:00:00 2019-01-17 00:00:00    5720      2019-01-16 00:00:00
191609   2019-01-15 00:00:00 2019-01-17 00:00:00    5720      2019-01-17 00:00:00
213156   2019-01-15 00:00:00 2019-01-16 00:00:00    2130      2019-01-15 00:00:00
213156   2019-01-15 00:00:00 2019-01-16 00:00:00    2130      2019-01-16 00:00:00

Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满天都是小星星 2025-02-18 07:00:29

您可以添加 pandas.date_range ,然后

(df
 .assign(ReferenceDate=[pd.date_range(a,d, freq='1d') for a,d in
                        zip(df['ArrivalDate'], df['DepartureDate'])])
 .explode('ReferenceDate')
)

输出:

   ClientID          ArrivalDate        DepartureDate  TotalRevenue ReferenceDate
0    191609  2019-01-15 00:00:00  2019-01-17 00:00:00          5720    2019-01-15
0    191609  2019-01-15 00:00:00  2019-01-17 00:00:00          5720    2019-01-16
0    191609  2019-01-15 00:00:00  2019-01-17 00:00:00          5720    2019-01-17
1    213156  2019-01-15 00:00:00  2019-01-16 00:00:00          2130    2019-01-15
1    213156  2019-01-15 00:00:00  2019-01-16 00:00:00          2130    2019-01-16

You can add the dates with pandas.date_range, then explode:

(df
 .assign(ReferenceDate=[pd.date_range(a,d, freq='1d') for a,d in
                        zip(df['ArrivalDate'], df['DepartureDate'])])
 .explode('ReferenceDate')
)

output:

   ClientID          ArrivalDate        DepartureDate  TotalRevenue ReferenceDate
0    191609  2019-01-15 00:00:00  2019-01-17 00:00:00          5720    2019-01-15
0    191609  2019-01-15 00:00:00  2019-01-17 00:00:00          5720    2019-01-16
0    191609  2019-01-15 00:00:00  2019-01-17 00:00:00          5720    2019-01-17
1    213156  2019-01-15 00:00:00  2019-01-16 00:00:00          2130    2019-01-15
1    213156  2019-01-15 00:00:00  2019-01-16 00:00:00          2130    2019-01-16
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文