我有以下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
我打算使用GroupBy子句进行转换以显示要呈现的数据的摘要视图。 (DELIVERY_DATETIME
是连接在一起并以逗号分隔的列。)在此示例中需要的结果:
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
尝试了GroupBy / drop_duplicates,但无法获得我想要的。我应该怎么做?
答案 0 :(得分:3)
IIUC groupby
和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