Dataframe Apply method to return multiple elements (series)

前端 未结 4 393
粉色の甜心
粉色の甜心 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 21:52

    the following solution to this frustratingly frustrating question works for me. I found the original suggestion in another StackOverflow post a while ago. The trick is to wrap up the return values into a Series like this:

    def divideAndMultiply(x,y):
        return pd.Series([x/y, x*y])
    

    Then this works as you wanted:

    df[['e','f']] = df.apply( lambda x: divideAndMultiply(x["a"],2) , axis =1)
    
    0 讨论(0)
  • 2021-01-19 22:02

    UPDATE

    Updated for version 0.23 - using result_type='broadcast' for further details refer to documentation

    Redefine your function like this:

    def divideAndMultiply(x,y):
        return [x/y, x*y]
    

    Then do this:

    df[['e','f']] = df.apply(lambda x: divideAndMultiply(x["a"], 2), axis=1, result_type='broadcast')
    

    You shall get the desired result:

    In [118]: df
    Out[118]:
       a  b  e  f
    0  0  1  0  0
    1  1  2  0  2
    2  2  3  1  4
    3  3  4  1  6
    
    0 讨论(0)
  • 2021-01-19 22:07
    df["e"], df["f"] = zip(*df.apply( lambda x: divideAndMultiply(x["a"],2) , axis =1))
    

    Should do the trick.

    (I show this example so you can see how to use multiple columns as the input to create multiple new columns)

    0 讨论(0)
  • 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)))
    
    0 讨论(0)
提交回复
热议问题