-2

I have the following table.

      time                   userid   market   device    query      querytype   browser
0     2020-07-01  04:47:21   A        EN-US    PC        WEATHER    WEATHER     EDGE
1     2020-07-01  07:23:52   C        ZH-CN    MOBILE    RECIPIES   FOOD        SAFARI
2     2020-07-01  15:32:57   D        EN-GB    TABLET    DOGS       ANIMALS     CHROME
3     2020-07-01  17:16:21   A        EN-CA    PC        SEATTLE    CITY        EDGE
4     2020-07-01  21:07:21   D        EN-GB    TABLET    DOG FOOD   ANIMAL      CHROME
5     2020-07-01  22:26:21   E        DE-DE    MOBILE    IPHONE     PRODUCTS    CHROME

And I am trying to answer the following question: Add a column to the table which contains the user's next query

I really do not have any clues. Please help

Dan_Lee
  • 87
  • 6
  • 1
    Hello, try to include as much relevant information and code snippets as you can please. In that way, we can better understand and help you – Yiannis Jul 01 '21 at 19:15

1 Answers1

0

You're looking for shift, see: Pandas compare next row Make sure you're sorted on time first

df['next_query'] = df.sort_values(['time']).groupby('userid')['query'].shift(-1)
                   time userid market  device     query querytype browser next_query
0  2020-07-01  04:47:21      A  EN-US      PC   WEATHER   WEATHER    EDGE    SEATTLE
1  2020-07-01  07:23:52      C  ZH-CN  MOBILE  RECIPIES      FOOD  SAFARI        NaN
2  2020-07-01  15:32:57      D  EN-GB  TABLET      DOGS   ANIMALS  CHROME   DOG FOOD
3  2020-07-01  17:16:21      A  EN-CA      PC   SEATTLE      CITY    EDGE        NaN
4  2020-07-01  21:07:21      D  EN-GB  TABLET  DOG FOOD    ANIMAL  CHROME        NaN
5  2020-07-01  22:26:21      E  DE-DE  MOBILE    IPHONE  PRODUCTS  CHROME        NaN
dww142
  • 96
  • 4