Query re Deprecation of groupby.agg() with a dictionary when renaming
See original GitHub issueCode Sample, a copy-pastable example if possible
# Your code here
df = pd.DataFrame({'City':['LA', 'NYC', 'NYC', 'LA', 'Chicago', 'NYC'],
'isFraud':[1, 0, 0, 1, 0, 1]})
df.groupby(df['City'])['isFraud'].agg({'Fraud':sum, 'Non-Fraud': lambda x: len(x)-sum(x), 'Squared': lambda x: (sum(x))**2})
Problem description
I just learnt using a dictionary for renaming in agg is going to be deprecated in the latest version. My question is what’s the alternative to achieve the above, i.e. using multiple lambda functions within agg?
Expected Output
Non-Fraud Fraud Squared
City
Chicago 1 0 0
LA 0 2 4
NYC 2 1 1
Output of pd.show_versions()
# Paste the output here pd.show_versions() here
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Rename result columns from Pandas aggregation ...
Use groupby apply and return a Series to rename columns · Renames the columns · Allows for spaces in the names · Allows...
Read more >Deprecation of using a dict in groupby().agg({...}) to rename ...
Hi all,. As a heads up, we are deprecating the use of a dictionary in the agg method on groupby/resample/rolling objects.
Read more >Group and Aggregate your Data Better using Pandas Groupby
Renaming of variables using dictionaries within the agg() function as in the diagram below is being deprecated/removed from Pandas – see notes. Aggregating ......
Read more >Applying different aggregate functions to different columns ...
Coding example for the question Applying different aggregate functions to different columns (now that dict with renaming is deprecated)-Pandas,Python.
Read more >What's new in 0.25.0 (July 18, 2019) - Pandas
These are the changes in pandas 0.25.0. ... when passing a dict to a Series groupby aggregation (Deprecate groupby.agg() with a dictionary when...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @jorisvandenbossche and @jreback for your answers. It seems there is no short alternative way to do this in one line. It’s sad to see such a handy feature to get removed.
So it looks like using a list of tuples, rather than a dict, is still supported?
This even looks to be supported when applying different functions to different columns, similar to the dict approach:
Will that continue to be supported?