查找行数最多的时间间隔

2024-09-28 22:39:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据框,其中每一行代表一辆自行车租赁:

(持续时间以秒为单位)

DataFrame bike rentals

我对熊猫和大数据非常陌生。我试图找出目前使用自行车最多的具体时间,以及最大值是多少。你知道吗

精确到小时和分钟的时间。*

租期从60秒到17270400秒不等(199天)

数据帧共有67.000行。你知道吗

我知道解决方案可能很琐碎,但我已经思考和寻找了一段时间,我坚持这个。你知道吗

以下是.csv的一些数据(从文件的顶部、中部和结尾选择了一些记录,以使数据具有一点多样性)

http://pastebin.com/Tgnupe7K

编辑:用.csv文件中的一些原始数据添加了pastebin


Tags: csv数据http结尾记录时间自行车单位
1条回答
网友
1楼 · 发布于 2024-09-28 22:39:20

这里的想法是考虑每辆自行车进入和退出使用的时间,表示进入使用为+1,退出使用为-1。取这些时间,对它们进行排序,然后在+1/-1上取一个累积和。累积和的最大值将给出给定时间内自行车的最大数量。你知道吗

我将使用我模拟的一些数据作为示例:

# Setup some fake data.
np.random.seed([3, 1415])
n = 67
df = pd.DataFrame({
    'start_date': np.random.choice(pd.date_range('2016-01-01', periods=10), size=n),
    'duration': np.random.randint(1, 10**5, size=n)
})
df['start_date'] += pd.to_timedelta(np.random.randint(1000, size=n), unit='m')

程序如下:

# Combine the entrance and exit times with the appropriate sign.
bike_times = pd.concat([
    pd.Series(1, index=df['start_date']),
    pd.Series(-1, index=df['start_date'] + pd.to_timedelta(df['duration'], unit='s')),
])

# Sort the dates and take the cumulative sum of the signs.
bike_times = bike_times.sort_index().cumsum()

# Find the max time and number of bikes.
max_dt = bike_times.idxmax()
max_bikes = bike_times.max()

在上面的代码中,max_dt将产生自行车达到最大值的开始时间。要找到结束时间,只需查看bike_times中的下一个索引值。你知道吗

相关问题 更多 >