我具有以下功能。我想将其应用于数据框的一行并返回2个值,然后将这2个值复制到2列中。我的两种方法都失败了(最后两行)
data1 = [['Alex',10,5,0],['Bob',12,4,1],['Clarke',13,6,0],['brke',15,1,0]]
df5 = pd.DataFrame(data1,columns=['Name','Age','weight','class'],dtype=float)
#print (df)
def calculate_distance2(row):
return pd.Series([row['Age']+row['weight'],row['Age']-row['weight']])
df5.apply(calculate_distance2, axis=1)
df5[['distance0'],['d6']]= df5.apply(calculate_distance2, axis=1)
df5['distance0'],df5['d6']= df5.apply(calculate_distance2, axis=1)
df5
答案 0 :(得分:1)
IIUC
def calculate_distance2(row):
return [row['Age']+row['weight'],row['Age']-row['weight']]
df5['distance0'],df5['d6']=calculate_distance2(df5)
df5
Name Age weight class distance0 d6
0 Alex 10.0 5.0 0.0 15.0 5.0
1 Bob 12.0 4.0 1.0 16.0 8.0
2 Clarke 13.0 6.0 0.0 19.0 7.0
3 brke 15.0 1.0 0.0 16.0 14.0