0

I know how to create calculations on columns

df['c'] = df['a'] * df['b']

but I'm not sure how to do it on data on different rows. For example, given this dataset:

ticker  date    close
17283   GSPC.INDX   2013-01-02  1462.4200
17284   GSPC.INDX   2013-01-03  1459.3700
17285   GSPC.INDX   2013-01-04  1466.4700
25074   IBM.US  2013-01-02  196.3500
25075   IBM.US  2013-01-03  195.2700
25076   IBM.US  2013-01-04  193.9900
30647   US10Y.INDX  2013-01-02  1.8402
30648   US10Y.INDX  2013-01-03  1.9214
30649   US10Y.INDX  2013-01-04  1.9013

How can I get for each date(2013-01-02,03,04,etc) a formula such as using the value of close of IBM.US * (GSPC.INDX * US10Y.INDX)

I was going to build a loop to go through it but I am hoping there's a easier way within pandas do something similar.

Lostsoul
  • 25,013
  • 48
  • 144
  • 239

1 Answers1

4

You could pivot your table:

df.pivot(index = 'date', columns = 'ticker', values = 'close')

Which outputs:

date        GSPC.INDX   IBM.US  US10Y.INDX
2013-01-02  1462.42     196.35  1.8402
2013-01-03  1459.37     195.27  1.9214
2013-01-04  1466.47     193.99  1.9013

Then you can see you can perform your calculations using columns.

dm2
  • 4,053
  • 3
  • 17
  • 28