How to apply a function with multiple arguments to a dataframe? My function is something like this:
def test(a,b,c,d,e,f):
if b == 1 :
if d == 0 :
return a-f
if b == 0 :
if c == 1 :
return a-e
My sample data is this :
| a | b | c | d | e | f |
|---|---|---|---|---|---|
| 2021-04-29 22:04:000 | 1 | 0.0 | 0.0 | 2021-04-28 10:10:00 | 2021-04-29 23:14:00 |
| 2021-06-10 06:00:00 | 0 | 1.0 | 1.0 | 2021-06-09 23:00:00 | 2021-06-11 00:29:00 |
| 2021-06-09 23:00:00 | 1 | 0.0 | 0.0 | 2021-06-06 11:00:00 | 2021-06-10 06:00:00 |
| 2021-06-06 11:00:00 | 0 | 1.0 | 1.0 | 2021-06-06 08:00:00 | 2021-06-09 23:00:00 |
| 2021-06-06 08:00:00 | 1 | 0.0 | 0.0 | 2021-06-06 04:00:00 | 2021-06-06 11:00:00 |
I tried this line of code and then shows error : the code:
df['dt'] = df.apply(test, args=('a','b','c','d','e','f'), axis=1)
the error:
test() takes 6 positional arguments but 7 were given
I don't understand why it detected as 7 arguments when in my code only six.
argsare additional. In this case you've passed the literal charactersa-fas additional arguments.