我是 Python 初学者,我有以下代码:levage_mean = df.groupby('cum_cycle_id')['levage'].mean().rename("levage_mean")levage_min = df.groupby('cum_cycle_id')['levage'].min().rename("levage_min")levage_max = df.groupby('cum_cycle_id')['levage'].max().rename("levage_max")levage_sum = df.groupby('cum_cycle_id')['levage'].sum().rename("levage_sum")load_min= df.groupby('cum_cycle_id')['load'].min().rename("load_min")load_max = df.groupby('cum_cycle_id')['load'].max().rename("load_max")load_mean = df.groupby('cum_cycle_id')['load'].mean().rename("load_mean")load_sum = df.groupby('cum_cycle_id')['load'].sum().rename("load_sum")load_median = df.groupby('cum_cycle_id')['load'].median().rename("load_median")`我想把它放在一个循环中,因为它太重复了,我该怎么办?谢谢您的回复!
1 回答
繁华开满天机
TA贡献1816条经验 获得超4个赞
你可以试试这个:
init_list = []
my_list = ['leaf_', 'load_']
mean_min_max_list = ['mean', 'min', 'max', 'sum']
for item in my_list:
for ele in mean_min_max_list:
curr = df.groupby('cum_cycle_id')[item].mean().rename(item + ele)
init_list.append(curr)
添加回答
举报
0/150
提交
取消