Vega
Vega

Reputation: 2930

How to apply a complicated function a function on a column without “apply”?

I have a dataframe df:

A    | B   | C   | ...         | D
1000 | 600 | 600 | productdesc | 0
1500 | 400 | 600 | productdesc | 1
1000 | 600 | 300 | productdesc | 0

and a function do_stuff():

def do_stuff(A, B, C):
    * calculations *
    return result

I would like to apply this function onto my dataframe df. Due to the size of my dataframe and the complexity of my function I try to avoid .apply().

Is there any other method to use a function on a dataframe with column values of each row as function parameters for the result for each row into a new column? Something like

df["scale_factor"] = do_stuff(df[["A", "B", "C"]])

End result should be:

A    | B   | C   | ...         | D | scale_factor
1000 | 600 | 600 | productdesc | 0 | *result of do_stuff(1000, 600, 600)*
1500 | 400 | 600 | productdesc | 1 | *result of do_stuff(1500, 400, 600)*
1000 | 600 | 300 | productdesc | 0 | *result of do_stuff(1000, 600, 300)*

Upvotes: 0

Views: 36

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31236

Just need to ensure you return a list or np.array of same size as data frame

df = pd.DataFrame({f"col{i}":[random.randint(0,10) for i in range(10)] for i in range(4)})

def dostuff(a):
    return [f"*result of dostuff({x},{a[1][i]},{a[2][i]})*" for i,x in enumerate(a[0])]

df["scale_factor"] = dostuff(np.array(df[["col0","col1","col2"]]).T)
print(df.to_string(index=False))

output

 col0  col1  col2  col3                 scale_factor
    2     0     3     2   *result of dostuff(2,0,3)*
    9     6    10     2  *result of dostuff(9,6,10)*
    0     7     8     4   *result of dostuff(0,7,8)*
   10     2     9     6  *result of dostuff(10,2,9)*
    8     3     4     2   *result of dostuff(8,3,4)*
    2     2     2     5   *result of dostuff(2,2,2)*
    1     8     1     5   *result of dostuff(1,8,1)*
    0     1     6     6   *result of dostuff(0,1,6)*
    2     0    10     6  *result of dostuff(2,0,10)*
    9    10     8     2  *result of dostuff(9,10,8)*

Upvotes: 1

Related Questions