The code should work. BTW, you do not need to wrap f1 inside another lambda . Just go f1 . (They have exactly the same parameter signature)
>>> import pandas as pd >>> >>> def f1(s): ... return max(s, key=len) ... >>> data = pd.DataFrame([ ... {'id': 'GB', 'name': '"United Kingdom"'}, ... {'id': 'GB', 'name': 'England'}, ... {'id': 'US', 'name': '"United States"'}, ... {'id': 'US', 'name': 'America'}, ... ... ]) >>> data.groupby('id').agg({'name': f1}) name id GB "United Kingdom" US "United States"
source share