I Have a JSON type dictionary in Python like this:
d = {'2022-12-21 20:00:00': {'1. open': '135.5900',
'2. high': '135.7300',
'3. low': '135.5900',
'4. close': '135.6700',
'5. volume': '18031'},
'2022-12-21 19:45:00': {'1. open': '135.5700',
'2. high': '135.6000',
'3. low': '135.5500',
'4. close': '135.5700',
'5. volume': '4253'}}
and I would like to convert in into a pandas dataframe like:
|timestamp |open |high |low |close|volume
1 |2022-12-21 20:00:00 | 135 |135 | 135 | 135 |18031
2 |2022-12-21 19:45:00 | 134 | 112 | 123 | 231 |24124
I tried to do it with:
df = pd.DataFrame.from_dict(d, orient='index')
but after this the dates become the indexing column and I want an indexing column as well:
>Solution :
The dates become the index by default. You can reset the index and rename the dates column using the reset_index method:
import pandas as pd
d = {'2022-12-21 20:00:00': {'1. open': '135.5900',
'2. high': '135.7300',
'3. low': '135.5900',
'4. close': '135.6700',
'5. volume': '18031'},
'2022-12-21 19:45:00': {'1. open': '135.5700',
'2. high': '135.6000',
'3. low': '135.5500',
'4. close': '135.5700',
'5. volume': '4253'}}
df = pd.DataFrame.from_dict(d, orient='index')
df.reset_index(inplace=True, names="date")
print(df)
Gives:
date 1. open 2. high 3. low 4. close 5. volume
0 2022-12-21 20:00:00 135.5900 135.7300 135.5900 135.6700 18031
1 2022-12-21 19:45:00 135.5700 135.6000 135.5500 135.5700 4253
The inplace argument makes sure you modify the dataframe instead of creating a new one (the default), and the names argument renames the column that used to be index.
