我有一个数据集,显示谁在哪个时间预订了哪个房间,看起来是这样的
email room Start Date End Date
abc@corp.com L11M2 2021-02-01 08:00:00 2021-02-01 11:00:00
xyz@corp.com L12M4 2021-02-01 08:00:00 2021-02-01 10:00:00
我想把它分成不同的小时,这样一行只包含一小时的数据。 这是我想要的数据帧
email room Start Date End Date
abc@corp.com L11M2 2021-02-01 08:00:00 2021-02-01 09:00:00
abc@corp.com L11M2 2021-02-01 09:00:00 2021-02-01 10:00:00
abc@corp.com L11M2 2021-02-01 10:00:00 2021-02-01 11:00:00
xyz@corp.com L12M4 2021-02-01 08:00:00 2021-02-01 09:00:00
xyz@corp.com L12M4 2021-02-01 09:00:00 2021-02-01 10:00:00
有什么方法可以在python中实现这一点吗
下面是一个使用
pandas.date_range
和explode
的简单解决方案:输出:
让我们创建一些示例数据
数据如下所示
您可以使用
apply
函数拆分每一行,确保返回一个pd.Series
在这之后,
new_start
是每小时的开始,有一个双索引,一个是原始索引,一个是特定块的顺序,也可能有用现在只需将其连接到原始帧
您可以像这样添加
end
列pandas
melt
与pyjanitor的complete
的组合可以帮助转换数据:相关问题 更多 >
编程相关推荐