gpt4 book ai didi

python - 根据列中的值重复行 x 次

转载 作者:太空狗 更新时间:2023-10-29 18:28:33 24 4
gpt4 key购买 nike

我有一个酒店预订的 Pandas 数据框。每一行都是一个预订,如下所示:

Name             Arrival       Departure     RoomNights
Trent Cotchin 29/10/2017 2/11/2017 4
Dustin Martin 1/11/2017 4/11/2017 3
Alex Rance 2/11/2017 3/11/2017 1

我想用python转换,让每一行都变成roomnight。输出看起来像这样:

Name             Arrival       Departure     RoomNights   RoomNight Date
Trent Cotchin 29/10/2017 2/11/2017 4 29/10/2017
Trent Cotchin 29/10/2017 2/11/2017 4 30/10/2017
Trent Cotchin 29/10/2017 2/11/2017 4 31/10/2017
Trent Cotchin 29/10/2017 2/11/2017 4 1/11/2017
Dustin Martin 1/11/2017 4/11/2017 3 1/11/2017
Dustin Martin 1/11/2017 4/11/2017 3 2/11/2017
Dustin Martin 1/11/2017 4/11/2017 3 3/11/2017
Alex Rance 2/11/2017 3/11/2017 1 2/11/2017

这让我可以轻松地计算任何给定日期/月份的房夜总数。

最佳答案

使用:

#convert columns to datetime
df['Arrival'] = pd.to_datetime(df['Arrival'])
df['Departure'] = pd.to_datetime(df['Departure'])

#repeat rows
df = df.loc[df.index.repeat(df['RoomNights'])]
#group by index with transform for date ranges
df['RoomNight Date'] =(df.groupby(level=0)['Arrival']
.transform(lambda x: pd.date_range(start=x.iat[0], periods=len(x))))
#unique default index
df = df.reset_index(drop=True)
print (df)
Name Arrival Departure RoomNights RoomNight Date
0 Trent Cotchin 2017-10-29 2017-11-02 4 2017-10-29
1 Trent Cotchin 2017-10-29 2017-11-02 4 2017-10-30
2 Trent Cotchin 2017-10-29 2017-11-02 4 2017-10-31
3 Trent Cotchin 2017-10-29 2017-11-02 4 2017-11-01
4 Dustin Martin 2017-11-01 2017-11-04 3 2017-11-01
5 Dustin Martin 2017-11-01 2017-11-04 3 2017-11-02
6 Dustin Martin 2017-11-01 2017-11-04 3 2017-11-03
7 Alex Rance 2017-11-02 2017-11-03 1 2017-11-02

关于python - 根据列中的值重复行 x 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46659378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com