question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query re Deprecation of groupby.agg() with a dictionary when renaming

See original GitHub issue

Code 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:closed
  • Created 6 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
allen-qcommented, May 14, 2017

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.

5reactions
bridwellcommented, Mar 14, 2018

So it looks like using a list of tuples, rather than a dict, is still supported?

df.groupby(df['City'])['isFraud'].agg([
    ('Fraud', sum),
    ('Non-Fraud', lambda x: len(x)-sum(x)),
    ('Squared', lambda x: (sum(x))**2)
])
        Fraud   Non-Fraud   Squared
City            
Chicago     0           1          0
LA          2           0          4
NYC         1           2          1

This even looks to be supported when applying different functions to different columns, similar to the dict approach:

df['severity'] = np.arange(len(df))

df.groupby(df['City']).agg({
    'isFraud': [
        ('Fraud', sum),
        ('Non-Fraud', lambda x: len(x)-sum(x)),
        ('Squared', lambda x: (sum(x))**2)
    ], 
    'severity': [('avg severity', 'mean')]    
})
            isFraud                         severity
        Fraud   Non-Fraud   Squared     avg severity
City                
Chicago     0    1          0           4.000000
LA          2    0          4           1.500000
NYC         1    2          1           2.666667

Will that continue to be supported?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found