morteza
morteza

Reputation: 153

Python pandas perform same aggregation on all other columns, whithout naming columns

According to this thread, when you want to apply the same aggregation function to multiple columns, you have to name columns. now consider a situation which i have many columns (30 columns for example). is there any way to do aggregation without naming columns? i mean is there any thing like this?

import pandas as pd
df = pd.DataFrame(...)
df.groupby('id').agg(lambda: col -> [sum(col) if col != id]);

Upvotes: 0

Views: 246

Answers (1)

Ignatius Reilly
Ignatius Reilly

Reputation: 1750

The solution you linked uses df.groupby('id')['x1', 'x2'].agg('sum').

So, to use every one of many columns but except a few ones:

columns_to_exclude = ['year', 'month' ,'day']
columns_to_aggregate = [col for col in df.columns if col not in columns_to_exclude]
df.groupby('id')[columns_to_aggregate].agg('sum')

Upvotes: 1

Related Questions