gpt4 book ai didi

python - 使用 pandas 根据其他列的值获取最新值

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:32 25 4
gpt4 key购买 nike

我有一个包含预订和搜索的数据集,按用户排序。我有一列在预订时为真,在搜索时为假,我需要添加另一列“previous_booking”,它需要具有最新预订的值。

   user    visit_type  booking  search  visit_num         hotel
0 user1 busqueda False True 1 NaN
1 user1 busqueda False True 2 NaN
2 user1 reserva True False 3 15
3 user2 busqueda False True 1 NaN
4 user2 busqueda False True 2 NaN
5 user2 reserva True False 3 156
6 user2 reserva True False 3 156
7 user2 busqueda False True 4 NaN
8 user2 busqueda False True 5 NaN
9 user2 reserva True False 6 123

我需要这样的东西:

user visit_type  booking  search  visit_num       hotel         latest_booking
0 user1 busqueda False True 1 NaN NaN
1 user1 busqueda False True 2 NaN NaN
2 user1 reserva True False 3 15 NaN
3 user2 busqueda False True 1 NaN NaN
4 user2 busqueda False True 2 NaN NaN
5 user2 reserva True False 3 156 NaN
6 user2 reserva True False 3 156 156
7 user2 busqueda False True 4 NaN NaN
8 user2 busqueda False True 5 NaN NaN
9 user2 reserva True False 6 123 156
10 user2 reserva True False 6 135 123

最佳答案

使用 shift + ffill,然后将 booking = False 行转换为 NaN

df['latest_booking'] = df.groupby('user')['hotel'].apply(lambda x: x.shift().ffill())
df.loc[~df['booking'], 'latest_booking'] = np.nan

print(df)

user visit_type booking search visit_num hotel latest_booking
0 user1 busqueda False True 1 NaN NaN
1 user1 busqueda False True 2 NaN NaN
2 user1 reserva True False 3 15.0 NaN
3 user2 busqueda False True 1 NaN NaN
4 user2 busqueda False True 2 NaN NaN
5 user2 reserva True False 3 156.0 NaN
6 user2 reserva True False 3 156.0 156.0
7 user2 busqueda False True 4 NaN NaN
8 user2 busqueda False True 5 NaN NaN
9 user2 reserva True False 6 123.0 156.0

关于python - 使用 pandas 根据其他列的值获取最新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50756894/

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