简体   繁体   中英

How to keep only the most recent revised order for each order in Pandas

Say I have a data frame that tracks the order number, and the revision number for that order in two different columns like so:

OrderNum  RevNum  TotalPrice
 0AXL3     0       $5.00
 0AXL3     1       $4.00
 0AXL3     2       $7.00
 0AXL3     3       $8.00
 0BDF1     0       $3.00
 0BDF1     1       $2.50
 0BDF1     2       $8.50

The result we want is a new data frame that only has the most recent version of each order, so :

OrderNum  RevNum  TotalPrice
 0AXL3     3       $8.00
 0BDF1     2       $8.50

Is there a quick way to do this in pandas?

IIUC:

In [100]: df.groupby('OrderNum', as_index=False).last()
Out[100]:
  OrderNum  RevNum TotalPrice
0    0AXL3       3      $8.00
1    0BDF1       2      $8.50

UPDATE:

If there were other columns in the data frame, would this keep those as well?

In [116]: df['new'] = np.arange(len(df))

In [117]: df
Out[117]:
  OrderNum  RevNum TotalPrice  new
0    0AXL3       0      $5.00    0
1    0AXL3       1      $4.00    1
2    0AXL3       2      $7.00    2
3    0AXL3       3      $8.00    3
4    0BDF1       0      $3.00    4
5    0BDF1       1      $2.50    5
6    0BDF1       2      $8.50    6

In [118]: df.groupby('OrderNum', as_index=False).last()
Out[118]:
  OrderNum  RevNum TotalPrice  new
0    0AXL3       3      $8.00    3
1    0BDF1       2      $8.50    6

One way is use drop_duplicates, note dataframe should be sorted on RevNum from smallest to largest or you can add sort_values:

df1.drop_duplicates(subset='OrderNum', keep='last')

Output:

  OrderNum  RevNum TotalPrice
3    0AXL3       3      $8.00
6    0BDF1       2      $8.50

OR

df1[~df1.duplicated(subset='OrderNum', keep='last')]

Output:

  OrderNum  RevNum TotalPrice
3    0AXL3       3      $8.00
6    0BDF1       2      $8.50

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