Dataframe Apply method to return multiple elements (series)

前端 未结 4 394
粉色の甜心
粉色の甜心 2021-01-19 21:15
import pandas as pd

Let\'s say I have a dataframe like so:

df = pd.DataFrame({\"a\":range(4),\"b\":range(1,5)})
         


        
4条回答
  •  失恋的感觉
    2021-01-19 22:15

    Almost there. Use zip* to unpack the function. Try this:

    def divideAndMultiply(x,y):
        return x/y, x*y
    
    df["e"], df["f"] = zip(*df.a.apply(lambda val: divideAndMultiply(val,2)))
    

提交回复
热议问题