Applying Function On Multiple Columns To Create Multiple New Columns
I need to apply a function on a df to create multiple new columns. As an input to my function I would need (i) a row or (ii) multiple columns def divideAndMultiply(x,y): return
Solution 1:
UPDATED
This is a straightforward example with a Quandl dataset,
import quandl
df = quandl.get("WIKI/GOOGL")
columns = ["High", 'Low', 'Close']
def operations(row, columns):
df1 =row[columns[0]] +row[columns[1]] +row[columns[2]]
df2 =-row[columns[1]] -row[columns[2]] +row[columns[0]]
return df1, df2
df["function1"], df["function2"] = zip(*df.apply(lambda row: operations(row, columns), axis=1))
df[["High","Low","Close","function1", "function2"]].head(5)
Post a Comment for "Applying Function On Multiple Columns To Create Multiple New Columns"