Assuming a pandas Dataframe as:
import pandas as pd
df = pd.DataFrame({'col1':[1,2,3,4,5,6],'col2':[11,22,33,44,55,66],'col3':[11,222,333,444,555,666]})
and a function like:
def sumit (a,b, name='me', password='pass'):
# pseudocode: open database with name 'me' and password 'pass'
# make queries and get c
c= 4
return a+b+c
How can I create an extra column 'colSUM' to apply the addition of columns col1 and col2 and the value c passing in the df as well the name and password as variables.
I can not figure it out from:
Passing a function with multiple arguments to DataFrame.apply
(applying vectorized logic)
or:
https://www.journaldev.com/33478/pandas-dataframe-apply-examples
Be aware that name and password can not be vectorized otherwise the connection with the database would not work.
before going with for loops I decided to give a try here!
The code I thought would work is:
df['new'] = df[['col1','col2']].apply(sumit, kwargs=(name='me', password='pass'))
nor this:
df['new'] = df[['col1','col2']].apply(sumit, name='me', password='pass')
nor:
df['new'] = df.apply(sumit, df['col1'], df['col2'], name='me', password='pass')
any idea? thanks