dataframe - Creating multiple new columns using one function in Pandas -


i know simple have been trying work out while, , need quite few functions.

i have dataframe 2 columns, both share price data.

i compute 2 new columns in new dataframe called 'returns', each column named same in first (i.e. 'aapl' , 'goog').

i use procedure original data , create 'data' dataframe:

names = ['aapl', 'goog']  def get_data(stock, start, end):     return web.get_data_yahoo(stock, start, end)['adj close']  data = pd.dataframe({n: get_data(n, '1/1/2009', '6/1/2012') n in names}) 

i know returns generate using (from pandas library):

returns = pd.dataframe(index=data.index) returns['*column a*'] = data['*column a*'].pct_change() 

however guessing need use sort of loop iterate on either 'names' or columns cannot work.

any appreciated. sorry if have been rather vague, first question , have searched 30 minutes through forum :)

you can use pct_change on entire df

in [15]: df = dataframe(np.random.randint(20,size=20).reshape(10,2),     columns=['aapl','goog'],index=date_range('20130101',periods=10))+50  in [16]: df out[16]:              aapl  goog 2013-01-01    53    54 2013-01-02    66    64 2013-01-03    50    59 2013-01-04    53    57 2013-01-05    67    65 2013-01-06    61    55 2013-01-07    68    52 2013-01-08    64    65 2013-01-09    62    62 2013-01-10    66    50  in [17]: 100*df.pct_change() out[17]:                   aapl       goog 2013-01-01        nan        nan 2013-01-02  24.528302  18.518519 2013-01-03 -24.242424  -7.812500 2013-01-04   6.000000  -3.389831 2013-01-05  26.415094  14.035088 2013-01-06  -8.955224 -15.384615 2013-01-07  11.475410  -5.454545 2013-01-08  -5.882353  25.000000 2013-01-09  -3.125000  -4.615385 2013-01-10   6.451613 -19.354839 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -