简体   繁体   English

如何将熊猫数据框与自身连接?

[英]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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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