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:
CodePudding user response:
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').reset_index(names="timestamp")
print(df)
The names
argument renames the column that used to be the index (new in version 1.5.0).
For older versions, use rename
:
df = pd.DataFrame.from_dict(d, orient='index').reset_index().rename(columns={'index': 'timestamp'})
Gives:
timestamp 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
CodePudding user response:
Create a new column df['new_index']=range(1, len(df) 1)
and then set that as the new index of the dataframe df.set_index('new_index')
. That should definitely work.
I would also be interested to find out what happens if you try df.reset_index()
.
You may need to set the name of the index to "timestamp" first df.index.name="timestamp"
.
CodePudding user response:
reset the index
self.stocks[s]= pd.DataFrame.from_dict(self.stocks[s],orient ='index').reset_index()