Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DataFrame .xlsx max()
#7
(Jan-22-2022, 10:52 PM)snippsat Wrote:
(Jan-22-2022, 06:28 PM)bnadir55 Wrote: 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 :
Yes,but what i mean that you should poste the a working Dataframe sample,the add image as additional info about wanted result.
Most of here work with Pandas sporadically or not all,then is difficult to answer if can not teste stuff out.
So i have to make the DataFrame to test stuff out.
import pandas as pd
from io import StringIO

data = StringIO('''\
order id,order date,order requester,order urgency
1,10/1/2022,James,A
1,10/2/2022,Don,A
1,10/3/2022,Mike,B
2,10/4/2022,Mike,B
2,10/5/2022,Don,B
2,10/6/2022,James,B
3,10/7/2022,James,A
3,10/8/2022,Don,A
3,10/9/2022,Don,C
4,10/10/2022,Mike,C''')

df_1 = pd.read_csv(data, sep=',')
df_1["order date"] = pd.to_datetime(df_1["order date"])
df_2 = df_1.groupby(['order id'],as_index=False)[['order date']].max()
So now can merge DataFrames based on df_2 groupby result.
This match your wanted result in image.
>>> df_3 = df_1.merge(df_2, on=['order id', 'order date'], how='inner')
>>> df_3
   order id order date order requester order urgency
0         1 2022-10-03            Mike             B
1         2 2022-10-06           James             B
2         3 2022-10-09             Don             C
3         4 2022-10-10            Mike             C

Got you, thank you!
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