简体   繁体   English

熊猫在df中找到与上一行具有相同值的最后一行

[英]pandas find the last row with the same value as the previous row in a df

I have a df , 我有一个df

acct_no    code    date           id
100        10      01/04/2019     22
100        10      01/03/2019     22
100        10      01/05/2019     22
200        20      01/06/2019     33
200        20      01/05/2019     33
200        20      01/07/2019     33

I want to first sort the df in ascending order for date when acct_no and code are the same, acct_nocode相同时,我想按date升序对df进行排序,

df.sort_values(['acct_no', 'code', 'date'], inplace=True)

then I am wondering what the way to find the last row whose acct_no , code are the same as the previous row, the result need to look like, 那么我想知道如何找到最后一行acct_nocode与上一行相同的结果,结果需要看起来像什么,

  acct_no    code    date           id
  100        10      01/05/2019     22
  200        20      01/07/2019     33

You can also try with groupby.last() : 您也可以尝试使用groupby.last()

df.groupby(['acct_no', 'code'],as_index=False).last()

   acct_no  code        date  id
0      100    10  01/05/2019  22
1      200    20  01/07/2019  33

Use DataFrame.drop_duplicates , but first convert column to datetimes: 使用DataFrame.drop_duplicates ,但首先将列转换为日期时间:

#if dates are first use dayfirst=True
df['date'] = pd.to_datetime(df['date'], dayfirst=True)
#if months are first
#df['date'] = pd.to_datetime(df['date'])
df1 = (df.sort_values(['acct_no', 'code', 'date'])
         .drop_duplicates(['acct_no', 'code'], keep='last'))
print (df1)
   acct_no  code       date  id
2      100    10 2019-05-01  22
5      200    20 2019-07-01  33

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

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