Pandas apply tuple unpack function on multiple col

2019-07-26 16:57发布

Given a function that takes multiple arguments and returns multiple values as so:

def tuple_unpack(value, another_value):
  ''' does some interesting stuff ... '''
   return value, another_value

Is there a way to apply such function to a pandas dataframe where for the 2 function arguments I can pass values from 2 columns, then unpack the output tuple on multiple colums as so:

df[['value_col','another_value_col']] = df.apply(lambda df.col, df.col: tuple_unpack)  

1条回答
Rolldiameter
2楼-- · 2019-07-26 17:39

You can using concat , with dataframe constructor

unpackdf=pd.DataFrame(df.apply(lambda x : tuple_unpack(x.col_1,x.col_2),1).tolist(),columns=['col1','col2'],index=df.index)
yourdf=pd.concat([unpackdf,df],axis=1)
查看更多
登录 后发表回答