Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DataFrame .xlsx max()
#5
Photo 
(Jan-22-2022, 11:47 AM)snippsat Wrote: Try:
df = df[df.groupby(['order id'],as_index=False)[['order date']].max()]

(Jan-22-2022, 05:46 PM)snippsat Wrote: You should post a sample of the DataFrame so can run it,then it easier to test stuff out.
If i make example,so can run your line of code.
import pandas as pd

mydataset = {
  'order id': [3, 7, 2],
  'order date': ['2020-01-01', '2020-01-02', '2020-01-03'],
  'order requester': ['a', 'b', 'c'],
  'order urgency': ['slow', 'fast', 'now']
}

df_1 = pd.DataFrame(mydataset)
df_1["order date"] = pd.to_datetime(df_1["order date"])
df_2 = df_1.groupby(['order id'],as_index=False)[['order date']].max()

print(df_1)
print('-' * 40)
print(df_2)
Output:
order id order date order requester order urgency 0 3 2020-01-01 a slow 1 7 2020-01-02 b fast 2 2 2020-01-03 c now ---------------------------------------- order id order date 0 2 2020-01-03 1 3 2020-01-01 2 7 2020-01-02
So this is just guess of your data,can try combine back group date(may not be what you want).
>>> df_2.combine_first(df_1)
  order date  order id order requester order urgency
0 2020-01-03         2               a          slow
1 2020-01-01         3               b          fast
2 2020-01-02         7               c           now

Thx,
here is what I need return the left table with all the rows and columns that carry the max(Order_date) grouped by order_ID (see results on the right table)
see in attachment :

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
DataFrame .xlsx max() - by bnadir55 - Jan-22-2022, 10:55 AM
RE: DataFrame .xlsx max() - by snippsat - Jan-22-2022, 11:47 AM
RE: DataFrame .xlsx max() - by bnadir55 - Jan-22-2022, 11:53 AM
RE: DataFrame .xlsx max() - by snippsat - Jan-22-2022, 05:46 PM
RE: DataFrame .xlsx max() - by bnadir55 - Jan-22-2022, 06:28 PM
RE: DataFrame .xlsx max() - by snippsat - Jan-22-2022, 10:52 PM
RE: DataFrame .xlsx max() - by bnadir55 - Jan-23-2022, 10:05 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020