简体   繁体   中英

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?

Use agg , as in:

df = pd.DataFrame({"a": range(3), "b": range(3,6)})
df.agg({"a": sum, "b": np.mean})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM