Lets say I have a data frame like this:
df =
X
0 17
1 120
2 -290
Now if I want to generate three new columns viz., X+12
, X-12
, and X+100
from X
. The final df will have 4 columns: X
, X+12
, X-12
, and X+100
.
How do I do this in a clean way? .apply
only transforms a column into another. How can I use this function to transform one column into multiple columns? Is there a way to do this?
I'm looking for Pandas way of doing this preferably using .apply or any other suitable function.
EDIT: Forgot to add some more info. I have a function that takes this value and generates a vector of values from this value. I should be able to use this with something like:
df[['X1','X2','X3']] = df.X.apply(f)
where f
would be:
def f(x):
return x+12, x-12, x+100
I want to be able to generate columns this way. Is this possible?