I have a dataframe I am adding new columns to. I am doing this using output from zip that uses output from a function. Seen below:
The function generates 4 new columns that I am trying to add to an existing dataframe.
data = [
[1, 123],
[2, 123454],
[3, 64564],
]
df = pd.DataFrame(data, columns=["ID", "number"])
# function
def func(num):
double = num * 2
triple = num * 3
quadruple = num * 4
tenex = num * 10
return double, triple, quadruple, tenex
# apply function to create 4 new columns
df["double"], df["triple"], df["quad"], df["tenex"] = zip(
*df["number"].apply(lambda x: func(x))
)
Is there a more concise way to do this? It's fine when I am adding only 4 columns, but I want to expand this function to add 10+ columns.
I was considering something like this:
tuple(df[colname] for colname in col_list) = zip(
*df["number"].apply(lambda x: func(x))
)
but it doesn't work (error: SyntaxError: cannot assign function to call)