Quick Pandas DataFrame question... Just a conceptual question
Let's say I have a 3 column DataFrame. Call it df
:
A B C
0 1 2 3
1 1 2 3
2 1 2 3
3 1 2 3
4 1 2 3
Now let's say I have a function f(A,B,C)
, which in theory would take columns A
, B
, and C
as inputs. For example,
def function(A,B,C):
return (A+1, B/2, C*3)
This function returns a tuple, of course.
Essentially, I'd like to know if I could apply function
to df
to get the following output:
A B C
0 2 1 9
1 2 1 9
2 2 1 9
3 2 1 9
4 2 1 9
If so, how would I do that? I can't just type df.apply(function)
. I'll get a TypeError that says something like:
'function()' takes exactly 3 arguments (1 given)'
If I can't do that, I presume I would have to create individual functions? Like...
def f1(A):
return A+1
def f2(B):
return B/2
def f3(C):
return C*3