Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

dataframe groupby multiple columns

grouped_multiple = df.groupby(['Team', 'Pos']).agg({'Age': ['mean', 'min', 'max']})
grouped_multiple.columns = ['age_mean', 'age_min', 'age_max']
grouped_multiple = grouped_multiple.reset_index()
print(grouped_multiple)
Comment

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 :: count lines in file python 
Python :: flask get value of radio button 
Python :: what is the use of class in python 
Python :: python break long string multiple lines 
Python :: dunder pyhton 
Python :: left join outer apply 
Python :: python read parquet 
Python :: how to use virtual environment python 
Python :: install flask on linux mint for python3 
Python :: python palindrome 
Python :: python beginner practice problems 
Python :: increase colorbar ticksize 
Python :: user group template tag django 
Python :: python Non-UTF-8 code starting with 
Python :: Python NumPy swapaxis Function Example 2 
Python :: list comprehenstsion using lambda funcion 
Python :: ordered dictionary python 
Python :: stack data horizontally pandas 
Python :: pipenv with specific python version 
Python :: how to combine two arrays in python 
Python :: what is imageTk in pil python 
Python :: python version 
Python :: convert url to base64 image py 
Python :: delete rows with value in column pandas 
Python :: Converting List to Dataframe Using zip() function 
Python :: python turn off printing 
Python :: python Program for Sum of the digits of a given number 
Python :: create new list in for loop python 
Python :: django secure secret key 
Python :: qlistwidget item clicked event pyqt 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =