Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

python - Parameterizing a pandas group by

Is there a way to parameterize a pandas group by instead of passing in the hardcoded list?

group_by_cols = "id","week_number"
aggregate_cols = "col1","col2","col3"
df = pd.read_csv(input_file_name)
df_total = df.groupby([group_by_cols])[aggregate_cols].sum()

Is this possible?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If want pass lists remove [] from [group_by_cols] for nested lists:

#for list added []
group_by_cols = ["id","week_number"]
aggregate_cols = ["col1","col2","col3"]

print (type(group_by_cols))
<class 'list'>

df = pd.read_csv(input_file_name)
df_total = df.groupby(group_by_cols)[aggregate_cols].sum()

Or if inputs are tuples convert them to lists like:

group_by_cols = "id","week_number"
aggregate_cols = "col1","col2","col3"

working same like passing tuples:

group_by_cols = ("id","week_number")
aggregate_cols = ("col1","col2","col3")

print (type(group_by_cols))
<class 'tuple'>

df = pd.read_csv(input_file_name)
df_total = df.groupby(list(group_by_cols))[list(aggregate_cols)].sum()

Sample data test:

df = pd.DataFrame({
        'id':list('aaaabb'),
         'week_number':[4,5,4,5,5,5],
         'col1':[7,8,9,4,2,3],
         'col2':[1,3,5,7,1,0],
         'col3':[5,3,6,9,2,4],
         'col4':[4,3,3,0,3,9]
})


group_by_cols = ["id","week_number"]
aggregate_cols = ["col1","col2","col3"]

df_total = df.groupby(group_by_cols)[aggregate_cols].sum()
print (df_total)
                col1  col2  col3
id week_number                  
a  4              16     6    11
   5              12    10    12
b  5               5     1     6

group_by_cols = "id","week_number"
aggregate_cols = "col1","col2","col3"

df_total = df.groupby(list(group_by_cols))[list(aggregate_cols)].sum()
print (df_total)
                col1  col2  col3
id week_number                  
a  4              16     6    11
   5              12    10    12
b  5               5     1     6

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...