+ 3

Two dataframe values to one function

I am trying to send the values from two columns to a function to return one value. Example: import pandas as pd def new(a , b): newt = a*b return newt df = pd.DataFrame([[25.6, 22, 17, 9], [3, 4, 18, 1], [17, 4, 33.3, 5], [7, 3, 9, 4]], columns=list('ABCD')) df[‘New calc’] = new(df[‘A’], df[‘B’]).map()

14th Jan 2019, 2:10 AM
James Sweatt
2 odpowiedzi
+ 2
Based on your example, this is how you can add a new column as a calculation from two existing ones: df['New calc'] = list(map(new, df['A'], df['B'])) I think it should work regardless what happens inside the actual transformation function, it can be an API call, map should take care of cycling through the values.
23rd Feb 2019, 9:24 AM
Tibor Santa
Tibor Santa - avatar
+ 1
I suppose that is a poor example. The project I am working on is on an intranet at work, so I can’t replicate it here. I’m not simply performing a new calculation. The values I need to send make and API call are in two different columns, and that will return just one new value.
14th Jan 2019, 9:30 AM
James Sweatt