人们在不同的时间到达和离开一个房间。考虑到他们的进出时间和高度,有什么有效的方法(在Python /Pandas中)可以随时找到房间里人的最大身高?
输入:
people = pd.DataFrame({
"height": [175, 180, 160],
"arrive": pd.to_datetime(["2017-04-05 14:20", "2017-04-05 13:10", "2017-04-05 10:30"]),
"depart": pd.to_datetime(["2017-04-05 17:54", "2017-04-06 07:56", "2017-04-05 11:04"])
})输出:
[["2017-04-05 10:30", 160], ["2017-04-05 11:04", None], ...]解说:第一个人在2017-04-05 10:30到达,他的身高是160,所以从那时起房间里的最大高度是160。这个人在11点04分离开房间,所以从那时起,最高身高就没有了。
这个问题类似于stackoverflow.com/q/47054341,但是解决方案并不令人满意,因为我们不应该将时间戳舍入最近的时间。
发布于 2022-10-24 16:38:07
我不确定这是一个有效的方法,但它可能会实现你想要的。
import panda as pd
people = pd.DataFrame({
"height": [175, 180, 160],
"arrive": pd.to_datetime(["2017-04-05 14:20", "2017-04-05 13:10", "2017-04-05 10:30"]),
"depart": pd.to_datetime(["2017-04-05 17:54", "2017-04-06 07:56", "2017-04-05 11:04"])
})
result = []
for time in sorted(list(people["arrive"]) + list(people["depart"])):
cond = (people["arrive"] <= time) & (people["depart"] > time)
result.append([str(time), people[cond]["height"].max()])
print(result)
"""
[['2017-04-05 10:30:00', 160],
['2017-04-05 11:04:00', nan],
['2017-04-05 13:10:00', 180],
['2017-04-05 14:20:00', 180],
['2017-04-05 17:54:00', 180],
['2017-04-06 07:56:00', nan]]
"""发布于 2022-10-24 16:14:07
IIUC
# melt to move columns as rows
df2=df.melt(id_vars='height', var_name='status', value_name='date')
# set height as None for departed time
df2['height']= df2['height'].mask(df2['status'].eq('depart'), 'None')
# groupby date and takes the max for each date
df2=df2.groupby('date', as_index=False)['height'].max()
df2date height
0 2017-04-05 10:30:00 160
1 2017-04-05 11:04:00 None
2 2017-04-05 13:10:00 180
3 2017-04-05 14:20:00 175
4 2017-04-05 17:54:00 None
5 2017-04-06 07:56:00 Nonehttps://stackoverflow.com/questions/74183415
复制相似问题