I need to use a function to calculate a new column for a table using existing data from its 4 columns.
Suppose I have a function that calculates orders, impressions, or clicks - anytging from different sources. Something like this:
def claculate_new_columns(complete_orders_a, total_a, completed_orders_b, total_b):
total = 0.0
#just some random calculations bellow - not important
source_a = complete_dorders_a + 1
test_a = total_a + 1
source_b = completed_orders_b + 1
test_b = total_b + 1
for i in something(smth):
total += source_a*test_a*source_b*test_b
return total
How do I use it with data from DataFrame columns?
I want to run over rows in columns and insert the results in a new column. Something like this (it doesn't work, obviously):
old_df['new_column'] = old_df.apply(claculate_new_columns(column1,column2,column3,column4))
Would be glad for a correct way to apply such functions to a DataFrame and use these DataFrame columns as function's arguments. What is the correct syntax?
Solutions from StackOverflow don't work for me probably because I searched for wrong answers.