Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce a data with the longest string under pandas framework?

Tags:

python

pandas

How to reduce a data with the longest string under pandas framework? I tried the following code, but get ValueError: invalid number of arguments.

def f1(s):
    return max(s, key=len)

data.groupby('id').agg({'name':(lambda s: f1(s)) })

Ex. Input

id name
GB "United Kingdom"
GB England
US "United States"
US America

Output:

id name
GB "United Kingdom"
US "United States"
like image 266
Wen Hsiao Avatar asked Dec 31 '14 03:12

Wen Hsiao


1 Answers

The code should work. BTW, you don't need to wrap f1 inside another lambda. Just pass f1. (They have exactly 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"
like image 168
falsetru Avatar answered Oct 17 '22 21:10

falsetru