Q:

Python: Efficently extract a single value for every group

I need to add a description column to a dataframe that is built by grouping items from another dataframe.
grouped= df1.groupby('item')
list= grouped['total'].agg(np.sum)
list= list.reset_index()

to assign a description label to every item I've come up with this solution:
def des(item):
        return df1['description'].loc[df1['item']== item].iloc[0]

list['description'] = list['item'].apply(des)

it works but it takes an enourmous amount of time to execute.
I'd like to do something like that
list=list.assign(description= df1['description'].loc[df1['item']==list['item']]

or
list=list.assign(description= df1['description'].loc[df1['item'].isin(list['item'])]

Theese are very wrong but hope you get the idea, hoping there is some pandas stuff that do the trick more efficently but can't find it 
Any ideas?

A:

I think you need DataFrameGroupBy.agg by dict of functions - for column total sum and for  description first:
df = df1.groupby('item', as_index=False).agg({'total':'sum', 'description':'first'})

Also dont use variable name list, because list is python code reserved word.
Sample:
df1 = pd.DataFrame({'description':list('abcdef'),
                   'B':[4,5,4,5,5,4],
                   'total':[5,3,6,9,2,4],
                   'item':list('aaabbb')})
print (df1)
   B description item  total
0  4           a    a      5
1  5           b    a      3
2  4           c    a      6
3  5           d    b      9
4  5           e    b      2
5  4           f    b      4

df = df1.groupby('item', as_index=False).agg({'total':'sum', 'description':'first'})
print (df)
  item  total description
0    a     14           a
1    b     15           d

