1

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?

prashu
  • 689
  • 2
  • 7
  • 10

2 Answers2

2

Afaik you can't address not yet existing columns via df[['X1','X2','X3']] = ... But the following would work:

def f(x):
  return x.add(12), x.sub(12), x.add(100)
df['X+12'], df['X-12'], df['X+100'] = f(df)
der_die_das_jojo
  • 893
  • 1
  • 9
  • 21
1

Just do as multiple statements:

In [31]:

df['X+12'], df['X-12'], df['X+100'] = df+12, df-12, df+100
df
Out[31]:
     X  X+12  X-12  X+100
0   17    29     5    117
1  120   132   108    220
2 -290  -278  -302   -190

EDIT

I think what you want to do won't work due to the shape of the returned values and expected return type. Another way would be to apply a lambda and concatenate the result:

In [66]:

pd.concat([df,df.X.apply(lambda s: pd.Series({'X+12':s+12, 'X-12':s-12, 'X+100':s+100}))], axis=1)

Out[66]:
     X  X+100  X+12  X-12
0   17    117    29     5
1  120    220   132   108
2 -290   -190  -278  -302

also @der_die_das_jojo is a decent answer too

EdChum
  • 376,765
  • 198
  • 813
  • 562
  • I just edited my problem to reflect the way I am looking for. – prashu Nov 27 '14 at 09:11
  • I just came across this SO question: http://stackoverflow.com/questions/16236684/apply-pandas-function-to-create-multiple-new-columns?rq=1 I think this suffices what I want to do. I'll have to change the return type of f as dict. – prashu Nov 27 '14 at 09:50
  • @prashu my edited answer (2nd) answer is based on that question, the point being it is not something that works automatically, you have to coerce the return type and then merge/concat afterwards – EdChum Nov 27 '14 at 09:54