Python 为什么在应用中返回两个值并不像在一个返回参数中那样设置整行?

Python 为什么在应用中返回两个值并不像在一个返回参数中那样设置整行?,python,pandas,Python,Pandas,我正在尝试通过pandas中的apply函数返回2个新列,我得到0和1,这是返回数据帧中的标题名称,当我得到2个数据帧时,我得到了包含2列的数据帧,我的代码是: import pandas as pd def sample_apply(x): numer_1, numer_2 = x[0], x[1] print numer_1/2, numer_2/2 return numer_1/2, numer_2/2 df = pd.DataFrame({ 'id':

我正在尝试通过pandas中的apply函数返回2个新列,我得到0和1,这是返回数据帧中的标题名称,当我得到2个数据帧时,我得到了包含2列的数据帧,我的代码是:

import pandas as pd

def sample_apply(x):
    numer_1, numer_2 = x[0], x[1]
    print numer_1/2, numer_2/2
    return numer_1/2, numer_2/2

df = pd.DataFrame({
    'id': 1,
    'number_1': 10,
    'number_2': 20,
},index=[0])


df['number_1_div_2'],df['number_2_div_2'] = df[['number_1', 'number_2']].apply(sample_apply, axis=1, result_type="expand")

print df
印刷品:

   id  number_1  number_2  number_1_div_2  number_2_div_2
0   1        10        20               0               1
而另一方面:

df1 = df[['number_1', 'number_2']].apply(sample_apply, axis=1, result_type="expand")
print df1a
印刷品

5 10

意味着申请有效,但作业无效,我做错了什么

我认为您希望分配给子集,如:

df[['number_1_div_2', 'number_2_div_2']] = df[['number_1', 'number_2']].apply(sample_apply, axis=1, result_type="expand")
print (df)
   id  number_1  number_2  number_1_div_2  number_2_div_2
0   1        10        20             5.0            10.0