Search code examples
pythonpandasaggregate

Rename Pandas .agg() columns inside function call


Is there a way to do the following inside the .agg() function?

hl = df[["sym", "bid", "ask"]].groupby("sym").agg(["min", "max"])
hl = hl.rename(columns={"min": "low", "max": "high"})

I see from the pandas documentation that you can use pd.namedAgg to apply different aggregations to different columns but all I would like to do is rename the columns in the original .agg() call.

I am used to writing in a very terse language (kdb+/q) hence wanted to reduce the number of lines of code.


Solution

  • What about this?

    import pandas as pd
    from random import randint
    
    # Create sample dataframe
    df = pd.DataFrame([
        {'sym': randint(1, 10), 'bid': randint(1, 10), 'ask': randint(1, 10)} for _ in range(10)
         ]).sort_values('sym').reset_index(drop=True)
    
    hl = df[["sym", "bid", "ask"]].groupby("sym").agg([('low', "min"), ('high', "max")])
    
    Before agg. After agg.
    input dataframe output dataframe