I have a dataframe with 3 columns a,b and c, and a function that takes 3 parameters, for instance a small example:
data_test = [[1,11,101],[2,12,102],[3,13,103],[4,14,104],[5,15,105],[6,16,106]]
df_test = pd.DataFrame(data_test,columns=['a','b','c'],dtype=float)
a b c
0 1.0 11.0 101.0
1 2.0 12.0 102.0
2 3.0 13.0 103.0
3 4.0 14.0 104.0
4 5.0 15.0 105.0
5 6.0 16.0 106.0
def my_function(a,b,c):
#changes a b and c and returns
x = a*10
y = b-20
z = a*b -7
return [x,y,z]
for each row I want to apply the function and return the values a,b,c,x,y,z in a new dataframe
I did:
df_wanted = pd.DataFrame( df_test.apply(lambda row: my_function(row['a'], row['b'], row['c']), axis=1) )
it is returning:
0
0 [10.0, -9.0, 4.0]
1 [20.0, -8.0, 17.0]
2 [30.0, -7.0, 32.0]
3 [40.0, -6.0, 49.0]
4 [50.0, -5.0, 68.0]
5 [60.0, -4.0, 89.0]
How to get result like below instead of arrays for each row:
a b c x y z
0 1.0 11.0 101.0 10.0 -9.0 4.0
1 2.0 12.0 102.0 20.0 -8.0 17.0
2 3.0 13.0 103.0 30.0 -7.0 32.0
3 4.0 14.0 104.0 40.0 -6.0 49.0
4 5.0 15.0 105.0 50.0 -5.0 68.0
5 6.0 16.0 106.0 60.0 -4.0 89.0
You can return a panda series instead of an array:
def my_function2(a,b,c):
#changes a b and c and returns
x = a*10
y = b-20
z = a*b -7
return pd.Series({
'x': x,
'y': y,
'z': z
})
df_wanted = pd.concat([
df_test,
df_test.apply(lambda row: my_function2(row['a'], row['b'], row['c']), axis=1)
], axis=1)
I know the function in your example is likely trivial, but try using vectorized functions to operate on columns instead of row-by-row. It's a lot more efficient.
Fix your code
df=df_test.join( pd.DataFrame( df_test.apply(lambda row: my_function(row['a'], row['b'], row['c']), axis=1).tolist() ,columns=list('xyz')))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.