使用pandas应用和用户定义的函数返回多个列

时间:2022-08-25 12:56:54

Say I have a function:

说我有一个功能:

def fn(x)
    y = x ** 2
    z = x ** 3
    return y, z

And I want to use df['x'].apply(lambda x: fn(x)) to return both y and z in separate columns. Is there a good way to do this by still using fn(x)? In reality, my function will be much more complicated - so I only want to run it once within the apply and assign output[0], output[1], etc to individual columns.

我想使用df ['x']。apply(lambda x:fn(x))在单独的列中返回y和z。有没有一种好方法可以通过仍然使用fn(x)来做到这一点?实际上,我的函数会复杂得多 - 所以我只想在apply中运行一次并将输出[0],输出[1]等分配给各个列。

1 个解决方案

#1


2  

How about this method? (n.b., I edited this answer in light of the comment below) so the apply step could take a single function with shared calculations and return the required series for the merge step.

这个方法怎么样? (n.b.,我根据下面的评论编辑了这个答案)所以应用步骤可以采用共享计算的单个函数,并返回合并步骤所需的系列。

data = {'state':['Ohio','Ohio','Ohio','Nevada','Nevada'], 'year':[2000,2001,2002,2001,2002],'pop':[1.5,1.7,3.6,2.4,2.9]}
frame = pd.DataFrame(data, columns = ['year','state','pop'])
def fn(x,head1,head2):
    y = x ** 2
    z = x ** 3
    return pd.Series({head1:y, head2:z}) 
frame = frame.merge(frame['pop'].apply(lambda s: fn(s,'xsqr','xcube')), left_index=True, right_index=True)

Results:

   year   state  pop   xcube   xsqr
0  2000    Ohio  1.5   3.375   2.25
1  2001    Ohio  1.7   4.913   2.89
2  2002    Ohio  3.6  46.656  12.96
3  2001  Nevada  2.4  13.824   5.76
4  2002  Nevada  2.9  24.389   8.41

#1


2  

How about this method? (n.b., I edited this answer in light of the comment below) so the apply step could take a single function with shared calculations and return the required series for the merge step.

这个方法怎么样? (n.b.,我根据下面的评论编辑了这个答案)所以应用步骤可以采用共享计算的单个函数,并返回合并步骤所需的系列。

data = {'state':['Ohio','Ohio','Ohio','Nevada','Nevada'], 'year':[2000,2001,2002,2001,2002],'pop':[1.5,1.7,3.6,2.4,2.9]}
frame = pd.DataFrame(data, columns = ['year','state','pop'])
def fn(x,head1,head2):
    y = x ** 2
    z = x ** 3
    return pd.Series({head1:y, head2:z}) 
frame = frame.merge(frame['pop'].apply(lambda s: fn(s,'xsqr','xcube')), left_index=True, right_index=True)

Results:

   year   state  pop   xcube   xsqr
0  2000    Ohio  1.5   3.375   2.25
1  2001    Ohio  1.7   4.913   2.89
2  2002    Ohio  3.6  46.656  12.96
3  2001  Nevada  2.4  13.824   5.76
4  2002  Nevada  2.9  24.389   8.41