简体   繁体   中英

How to join pandas dataframe with itself?

I have a dataframe in pandas that looks similar to this:

ApplicationId | Application Date | Account
1234          |  10/01/2018      | 12345
5678          |  10/30/2018      | 12345
9101          |  11/15/2018      | 12345
1213          |  10/01/2018      | 67891
1415          |  11/01/2018      | 67891
1617          |  10/01/2018      | 43210

I need to join the dataframe with itself to get the "next application date" based on Account and Application Date. SO the final result should be:

ApplicationId | Application Date | Account | Next Application Date
1234          |  10/01/2018      | 12345   | 10/30/2018
5678          |  10/30/2018      | 12345   | 11/15/2018
9101          |  11/15/2018      | 12345   | Nan
1213          |  10/01/2018      | 67891   | 11/01/2018
1415          |  11/01/2018      | 67891   | Nan
1617          |  10/01/2018      | 43210   | Nan

Could you please advise?

Thank you!

我认为这是groupby + shift问题

df['New']=df.groupby('Account')['Application Date'].shift(-1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM