1

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:

enter image description here

1
  • 1
    Try this: df.reset_index().rename(columns={'index': 'timestamp'}) Commented Dec 22, 2022 at 12:51

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

0

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".

1 Comment

i tried df.reset_index() too it doesnt change anything after that. any other way to index it rather than adding rows because some rows are going to get deleted after some operations
0

reset the index

self.stocks[s]= pd.DataFrame.from_dict(self.stocks[s],orient ='index').reset_index()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.