1

I have the following DataFrame:

FACTORY_ID  SUPPLIER_ID DELIVERY_DATETIME
A   1   05/09/2015 11:00
A   1   05/09/2015 11:00
A   2   05/09/2015 11:00
A   2   08/09/2015 11:00
A   1   08/09/2015 11:00
A   1   08/09/2015 11:00
A   1   08/09/2015 11:00
A   2   08/09/2015 11:00
A   2   13/09/2015 11:00
A   3   13/09/2015 11:00
A   3   13/09/2015 11:00
A   3   13/09/2015 11:00
A   3   13/09/2015 11:00
A   3   13/09/2015 11:00

which I intend to transform by using a GroupBy clause to show a summarized view of the data for presentation. (DELIVERY_DATETIME is a column that is concatenated and comma-delimited.) Desired results in this example:

FACTORY_ID  SUPPLIER_ID DELIVERY_DATETIME
A   1   05/09/2015  11:00:00 AM, 08/09/2015  11:00:00 AM
A   2   05/09/2015  11:00:00 AM, 13/09/2015  11:00:00 AM
A   3   13/09/2015  11:00:00 AM

Have tried GroupBy/drop_duplicates but have been unable to get what I wanted. How should I go about doing this?

1 Answer 1

3

IIUC groupby with agg

newdf=df.groupby(['FACTORY_ID','SUPPLIER_ID']).DELIVERY_DATETIME.agg(['first','last'])
newdf.loc[newdf['first']==newdf['last'],'last']=''
newdf
Out[69]: 
                             first        last
FACTORY_ID SUPPLIER_ID                        
A          1            05/09/2015  08/09/2015
           2            05/09/2015  13/09/2015
           3            13/09/2015            
Sign up to request clarification or add additional context in comments.

2 Comments

This might work. If I have multiple dates and would like to retrieve all dates, I could use 'nunique'. Just curious, how come first and last headers are at a level higher than the usual FACTORY_ID\SUPPLIER_ID? If I want it to be the same level (i.e. as a column) and reference it later for other purposes, is it still the same as accessing it via a df.column method?
ok i figured it out. This can be resolved via reset_index(). Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.