I am trying to convert/tranpose a very wide dataframe to a row-based dataframe (for lack of better word).
The origin table looks like this
stock_list = ['AAPL', 'TSLA', ' MSFT', 'COIN', 'META' ...... ]
df
date price_AAPLE volume_AAPLE price_TSLA volume_TSLA price_MSFT volume_MSFT ...
01-01 187 324234 517 5346 128 45143
01-02 124 234234 512 5436345 130 42345
I can easily transpose it in loop
cols = ['date', 'price', 'volume']
agg_df = pd.DataFrame()
for i in stock_list:
dataframe = df[['date', f'price_{i}', f'volume_{i}']]
dataframe.columns = cols
dataframe['stock'] = i
agg_df = agg_df.append(dataframe)
In this way the dataframe looks like:
date price volume stock
01-01 187 324234 AAPL
01-02 124 234234 AAPL
01-01 517 5346 TSLA
01-02 512 5436345 TSLA
.... .... .... ....
So my question is how to convert/transpose the table without using loop?
I got a really long list, I believe using loop is a bit slow here.
Thank you !!!!