I have a JSON file, which I then convert to a Pandas dataframe called stocks
. The stocks
dataframe is in wide format and I'd like to convert it to long format.
Here's what the stocks
dataframe looks like after it's ingested and converted from JSON:
TSLA MSFT GE DELL
0 993.22 320.72 93.19 57.25
I would like to convert the stocks
dataframe into the following format:
ticker price
0 TSLA 993.22
1 MSFT 320.72
2 GE 93.19
3 DELL 57.25
Here is my attempt (which works):
stocks = pd.read_json('stocks.json', lines=True).T.reset_index()
stocks.columns = ['ticker', 'price']
Is there a more Pythonic way to do this? Thanks!