Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python group by multiple aggregates

def f(x):
    d = {}
    d['a_sum'] = x['a'].sum()
    d['a_max'] = x['a'].max()
    d['b_mean'] = x['b'].mean()
    d['c_d_prodsum'] = (x['c'] * x['d']).sum()
    return pd.Series(d, index=['a_sum', 'a_max', 'b_mean', 'c_d_prodsum'])

df.groupby('group').apply(f)
Comment

pandas groupby multiple columns

df['COUNTER'] =1       #initially, set that counter to 1.
group_data = df.groupby(['Alphabet','Words'])['COUNTER'].sum() #sum function
print(group_data)
Comment

pandas groupby multiple columns

#formatting
candidates_salary_by_month =  candidates_df.groupby('month').agg(num_cand_month = 
                                                                ('num_candidates', 'sum'), 
                                                                avg_sal = ('salary', 'mean')).style.format('{:.0f}')

print(candidates_salary_by_month)
Comment

PREVIOUS NEXT
Code Example
Python :: numpy random matrix 
Python :: python install minio 
Python :: str replace python regex 
Python :: how to find the speed of a python program 
Python :: requests python3 example 
Python :: integer xticks 
Python :: python virtual enviroment 
Python :: python check if list 
Python :: add value to dictionary python 
Python :: how to print upto 5 decimal places in python 
Python :: python cli click 
Python :: how to get last n elements of a list in python 
Python :: list square python 
Python :: discord.py get server id 
Python :: smtplib send caleneder email 
Python :: from collections import defaultdict 
Python :: django reverse function 
Python :: how to add extra zeros after decimal in python 
Python :: add key if not exists python 
Python :: join to dataframes pandas 
Python :: python user input to tuple 
Python :: this figure includes axes that are not compatible with tight_layout, so results might be incorrect 
Python :: adding text cv2 
Python :: click a button using selenium python 
Python :: python pandas in list 
Python :: read data from s3 bucket python 
Python :: gogle query python simple 
Python :: python rock paper scissors 
Python :: convert pandas dataframe to dict with a column as key 
Python :: python input float 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =