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"
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With