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

时间:2018-12-10 16:23:54

标签: python pandas

我在熊猫中有一个与此相似的数据框:

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

我需要将数据框与自身结合起来,以根据帐户和申请日期获得“下一个申请日期”。所以最终结果应该是:

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

能请你指教吗?

谢谢!

1 个答案:

答案 0 :(得分:5)

我认为这是groupby + shift的问题

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