Pandas Transform and Filter

Split

Apply

df.groupby(['Name','City'])['Age'].apply(sum).reset_index()
or
df.groupby(['Name','City'])['Age'].sum().reset_index()

Combine

df['sum']=df.groupby(['Name','City'])['Age'].transform('sum')
df
import pandas as pd
import numpy as np
df = pd.DataFrame( {
"Name" : ["Alice", "Bob", "Mallory", "Mallory", "Bob" , "Mallory"] ,
"City" : ["Seattle", "Seattle", "Portland", "Seattle", "Seattle",
"Portland"] ,
"Age" : [30, 20, 25, 45, 20 , 15]} )

Filter Data with Transform

df[df.groupby(['Name','City'])['Age'].transform('sum') > 40]

Transform with Lambda

df['half_age']=df.groupby(['Name','City'])['Age'].transform(lambda x: x.sum()/2)
df

Filter with Pandas Groupby

df.groupby([‘Name’,’City’]).filter(lambda x: sum(x[‘Age’]) > 30)

df.groupby(['Name','City']).filter(lambda x: len(x) > 1)

use Map to create a new Column

df['Sum']=df.Name.map(df.groupby(['Name'])['Age'].sum())

see more

also

دیدگاهی بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *