Pandas 在多列上应用元组解包功能 [英] Pandas apply tuple unpack function on multiple columns
本文介绍了Pandas 在多列上应用元组解包功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定一个 function
,它接受多个参数并返回多个值,如下所示:
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
有没有办法将这样的函数应用到 pandas
dataframe
其中对于 2 个函数参数,我可以从 2 列传递值,然后将输出元组解包到多个列如下:
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)
推荐答案
你可以使用 concat
和 dataframe 构造器
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)
这篇关于Pandas 在多列上应用元组解包功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文