1 回答
TA贡献1834条经验 获得超8个赞
您可以使用该.apply()功能的文档与result_type='expand'
In [3]: df = pd.DataFrame({'one':[1,2,3,4], 'two':[5,6,7,8]})
In [4]: df.apply(lambda x: (sum(x), sum(x)), axis=1, result_type='expand')
Out[4]:
0 1
0 6 6
1 8 8
2 10 10
3 12 12
In [5]: df[['new', 'etc']] = df.apply(lambda x: (sum(x), sum(x)), axis=1, result_type='expand')
In [6]: df
Out[6]:
one two new etc
0 1 5 6 6
1 2 6 8 8
2 3 7 10 10
3 4 8 12 12
编辑:如果要显示进度,请单独定义应用功能即
def func(row):
print(row)
return sum(row), sum(row)
In [3]: df = pd.DataFrame({'one':[1,2,3,4], 'two':[5,6,7,8]})
In [4]: df.apply(func), axis=1, result_type='expand')
Out[4]:
0 1
0 6 6
1 8 8
2 10 10
3 12 12
In [5]: df[['new', 'etc']] = df.apply(lambda x: (sum(x), sum(x)), axis=1, result_type='expand')
In [6]: df
Out[6]:
one two new etc
0 1 5 6 6
1 2 6 8 8
2 3 7 10 10
3 4 8 12 12
添加回答
举报