organise pandas data frame like yahoo finance multiple stock dataframe

59 views Asked by At

I'm trying to organize my pandas data frame like the one we get from yahoo finance data frame of multiple stock symbols with OHLCV. how to achieve that.enter image description here

i tried.

df = df[['Name','datetime','open','high','low','close','volume']]
df.set_index(['Name', 'datetime'], inplace=True)
df = df.astype(float)

and got the following output.

enter image description here

1

There are 1 answers

11
jezrael On

It depends what need. If need all columns in MultiIndex in columns:

df1 = df.astype(float).unstack(0).swaplevel(0,1,axis=1).sort_index(axis=1)

If need processing only one column, e.g. open:

df2 = df.astype(float)['open'].unstack(0)