2

This works correctly:

import pandas as pd

def fnc(m):
    return m+4

df = pd.DataFrame({"m": [1,2,3,4,5,6], "c": [1,1,1,1,1,1], "x":[5,3,6,2,6,1]})
df
# apply a self created function to a single column in pandas
df["y"] = df['m'].apply(fnc)
df

I tried to modified code above. Here I need to add column m value to column c value and assign result to column y:

import pandas as pd

def fnc(m,c):
    return m+c

df = pd.DataFrame({"m": [1,2,3,4,5,6], "c": [1,1,1,1,1,1], "x":[5,3,6,2,6,1]})
df
# apply a self created function to a single column in pandas
df["y"] = df[['m','c']].apply(fnc)
df

gives me error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
d:\del\asfasf.py in 
      8 df
      9 # apply a self created function to a single column in pandas
----> 10 df["y"] = df[['m','c']].apply(fnc)
     11 df

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, raw, result_type, args, **kwds)
   6876             kwds=kwds,
   6877         )
-> 6878         return op.get_result()
   6879 
   6880     def applymap(self, func) -> "DataFrame":

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in get_result(self)
    184             return self.apply_raw()
    185 
--> 186         return self.apply_standard()
    187 
    188     def apply_empty_result(self):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
    294             try:
    295                 result = libreduction.compute_reduction(
--> 296                     values, self.f, axis=self.axis, dummy=dummy, labels=labels
    297                 )
    298             except ValueError as err:

pandas\_libs\reduction.pyx in pandas._libs.reduction.compute_reduction()

pandas\_libs\reduction.pyx in pandas._libs.reduction.Reducer.get_result()

TypeError: fnc() missing 1 required positional argument: 'c'

Question: How to correct my second code? If possible, please provide answer standard function syntax (not lambda function)

0

2 Answers 2

5

Add axis to consider in axis=1 in dataframe and access each column inside the function.Try this

def fnc(m):
    return (m.m+m.c)

df = pd.DataFrame({"m": [1,2,3,4,5,6], "c": [1,1,1,1,1,1], "x":[5,3,6,2,6,1]})
df["y"] = df[['m',"c"]].apply(fnc,axis=1)

or you can apply to df,without select "m" and "c" columns.

df["y"] = df.apply(fnc,axis=1)

output

Sign up to request clarification or add additional context in comments.

4 Comments

If I run df after your code I am receiving y column field with None. It should contain column with calculated values.
because in func it was print statement(mistake from me),and i changed it to return.now it should work.
Thank you. Can you please add explanation of this m.m+m.c part? What it means?
its extract the m and c column values and add together row wise.since we defined axis=1 in the apply function it enumerates row wise
2

try this, set axis to 1 for column wise operation & *x for un-packing arguments.

df["y"] = df[['m','c']].apply(lambda x : fnc(*x), axis=1)

4 Comments

What you think, is there any advantage over your version and df["y"] = df.apply(lambda row: fnc(row['m'], row['c']), axis = 1)?
No, both are the same *x is implicit where as row['m'], row['c'] is explicit.
In your code, what is passed to x parameter of lambda function? I really like it and want to understand it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.