In a dataframe with two columns I can easily create a third without a function if it is a numerical operation such as multiply df["new"] =df["one"] * df["two"].
However what if I need to pass in more than two parameters to a function and those parameters are columns from a dataframe.
Passing one column at a time is simple using: df.apply(my_func) but if the functions definition is, and requires three columns:
def WordLength(col1,col2,col3):
return max(len(col1),len(col2),len(col3))
For example, A function WordLength would return the maximum length of the word from any of the three columns passed into it.
I know for example this doesn't work but I imagine something like this to return a result of a function requiring three parameters into a dataframe column:
df["word_length"]= df.apply(WordLength, [[param1,param2,param3]])
Update Jon, when trying to use your method of passing in three parameters (values from three dataframe columns for a given row I am getting the following error:
def get(name,start_date,end_date):
try:
df = ...
response = df.apply(get, axis=1, args=('name', 'date', 'today'))
Error relating to arguments - I don't understand why it mentions 4 arguments when I have passed in three and the function only requires three arguments...
Error:
TypeError: ('getprice() takes exactly 3 arguments (4 given)', u'occurred at index 0')