如何执行以下for循环
operation = ['mean','std']
for i in range(0,len(operation)):
df2 = df.groupby(level='Index').agg(operation[i]).dropna(how='all')
....
但使用numpy中的函数
operation = ['np.mean','np.std']
for i in range(0,len(operation)):
df2 = df.groupby(level='Index').agg(operation[i]).dropna(how='all')
....
特别是我想使用ddof=1
答案 0 :(得分:1)
尝试传递实际函数而不是字符串,即:
operation = [np.mean, np.std]
要将参数传递给函数,可以使用函数参数传递lambda
。例如:
operation = [np.mean, lambda x: np.std(x, ddof=1)]