1

Let's say I have a dataframe like this:

 columnA columnB 0 10 90 1 83 17 2 30 21 ... 

and I have a function like this:

def my_func(a, b): value = #do some calculation return value 

Now I want to get a new column columnC for my dataframe based on the calculations of the function.
Obviously, df["columnC"]= my_func(df["columnA"], df["columnB"]) does not work.

What can I do to add the column?

4
  • 2
    use pandas apply : df['C'] = df.apply(my_func) Commented Oct 3, 2020 at 10:04
  • This also passes a pandas-series to my function. It is the same problem with the example I gave above. Commented Oct 3, 2020 at 10:06
  • 1
    Can you post what my_func does? Commented Oct 3, 2020 at 10:07
  • It's just some big if/return block Commented Oct 3, 2020 at 10:10

2 Answers 2

2

The right way to do it is:

df['c'] = df.apply(lambda row: my_func(row['a'], row['b']), axis=1) 
Sign up to request clarification or add additional context in comments.

Comments

0

I found a workaround. It's a bit hacky though...

df["columnC"]=pd.Series([my_func(row["columnA"], row["columnB"])for index, row in df.iterrows()], index=df.index) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.