I have a time series dataset stored in a dataframe, with multiple elements, for example stocks with their price, p/e ratio, and p/b ratio - so I have 3 rows per ticker/date. I'm wondering if there is a way to convert this, so I have one row for each ticker/date, and the price,p/e, and p/b as columns.
Sample dataframe:
import pandas as pd
dfts = pd.DataFrame({
'date': ['2020-01-01','2020-01-01','2020-01-01',
'2020-01-01','2020-01-01','2020-01-01',
'2020-01-02','2020-01-02','2020-01-02',
'2020-01-02','2020-01-02','2020-01-02'],
'ticker': ['AAPL','AAPL','AAPL',
'GOOGL','GOOGL','GOOGL',
'AAPL', 'AAPL', 'AAPL',
'GOOGL', 'GOOGL', 'GOOGL'],
'type': ['price','p/e','p/b',
'price','p/e','p/b',
'price','p/e','p/b',
'price','p/e','p/b'],
'value': [300,20,2,
1000,25,3,
310,21,2.1,
1110,26,2.9]
})
print(dfts)
I'm looking to convert this and get a result such as:
Date Ticker Price P/E P/B
2020-01-01 AAPL 300 20 2
2020-01-02 AAPL 310 21 2.1
2020-01-01 GOOGL 1000 25 3
2020-01-02 GOOGL 1110 26 2.6
Thanks