0

How would I use two columns as inputs to output a new column in pandas. For example how would I apply "some_function" in which Column A is the A input and Column B is the B input to create a new column E?

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,10,size=(6, 4)), columns=list('ABCD'))

def some_function(A, B):
  if A > 5:
    E = B - 7
  
  if A <= 5:
    E = B + 3

  return E
Nate Go
  • 7
  • 2
  • `df['E'] = df.apply(lambda x: some_function(x['A'], x['B']), axis=1)` or directly `df['E'] = some_function(df['A'], df['B'])` if the function accepts arrays as input – mozway Jan 11 '22 at 12:35
  • Here, you function only uses 2 values to produce one single value, and in the end you will generate a simple Series (a column). This is an appropriate use case for `agg` which may be more efficient than `apply`: `df[E] =df[['A','B']].agg(lambda x: some_function(*x), axis=1)` – Serge Ballesta Jan 11 '22 at 12:48

0 Answers0