I have a dataframe in the following format
symbol name date close market
0 BTC Bitcoin 2013-04-28 134.21 1500520000
1 BTC Bitcoin 2013-04-29 144.54 1491160000
2 BTC Bitcoin 2013-04-30 139.00 1597780000
3 BTC Bitcoin 2013-05-01 116.99 1542820000
4 BTC Bitcoin 2013-05-02 105.21 1292190000
...
symbol name date close market
752109 W3C W3Coin 2018-04-07 0.000277 0
752110 W3C W3Coin 2018-04-08 0.000286 0
752111 W3C W3Coin 2018-04-09 0.000270 0
752112 W3C W3Coin 2018-04-10 0.000286 0
752113 W3C W3Coin 2018-04-11 0.000300 0
I want to create a matrix with prices and a matrix with marketcaps. Because I'm interested in re-creating the S&P500 index. I have done this with a for loop below. Question: How do I do this without a for loop?
edit: Note that some symbols have different number of dates, for example BTC might have 1000 days but W3Coin only 50 days. I think that is what the error message is telling me.
import data:
df = pd.read_csv('data-coins-scrape/CryptoData.csv', parse_dates=True)
cols = ['symbol', 'name', 'date', 'close', 'market']
df = df[cols]
symbols = df.symbol.unique()
nr_coins = df.symbol.nunique()
for loop to get prices and makretcaps
prices = []
marketscaps = []
shapes = []
for s in symbols:
# slice out one coin
dfc = df.loc[df.symbol == s]
shapes.append(dfc.shape)
prices.append(dfc['close'])
marketcaps.append(df['market'])
del dfc
check the output
len(prices), len(marketcaps)
# (1531, 3065) is the output
type(prices[0]), prices[0].shape, prices[0].name
# output: (pandas.core.series.Series, (1810,), 'close')
df.loc[df.symbol == 'BTC', 'close'].head() == prices[0].head()
# returns True