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

时间:2018-06-08 08:55:31

标签: python pandas dataframe

我有一个包含预订和搜索的数据集,由用户订购。我有一列在预订时是真的而在搜索时是假的,我需要添加另一列'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

1 个答案:

答案 0 :(得分:1)

使用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