Dataframe Apply method to return multiple elements (series)
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: import pandas as pd Let's say I have a dataframe like so: df = pd.DataFrame({"a":range(4),"b":range(1,5)}) it looks like this: a b 0 0 1 1 1 2 2 2 3 3 3 4 and a function that multiplies X by Y: def XtimesY(x,y): return x*y If I want to add a new pandas series to df I can do: df["c"] =df.apply( lambda x:XtimesY(x["a"],2), axis =1) It works ! Now I want to add multiple series: I have this function: def divideAndMultiply(x,y): return x/y, x*y something like this ?: df["e"], df["f"] = df.apply( lambda x: divideAndMultiply(x["a"],2) , axis =1) It