[英]Iterating through columns in pandas while applying different functions to each column
Lets say I want to iterate though each column of a certain data frame, while at the same time applying different functions to each column.假设我想遍历某个数据帧的每一列,同时对每一列应用不同的函数。 If every function is different for each column, is there a way to automate the code and not write N lines of code, where N is the total number of columns?
如果每个 function 的每一列都不同,有没有办法自动化代码而不写 N 行代码,其中 N 是总列数?
Use agg
, as in:使用
agg
,如:
df = pd.DataFrame({"a": range(3), "b": range(3,6)})
df.agg({"a": sum, "b": np.mean})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.