How to reduce data with the longest string within pandas?

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

 def f1(s): return max(s, key=len) data.groupby('id').agg({'name':(lambda s: f1(s)) }) 

Ex. Enter

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

Conclusion:

 id name GB "United Kingdom" US "United States" 
+5
source share
1 answer

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" 
+5
source

Source: https://habr.com/ru/post/1210151/


All Articles