Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a more effective way to generate this dataframe?

I have a code which "converts" a dict into a pd.DataFrame. As result, I get the dataframe I need, but as I think code is not effective.

python
import datetime
import pandas as pd

data = {}
for index, row in get_data_row_by_row():
    data[index] = row

'''
As result i get something like

data = {"2022-04-22": {"Open": 4268.169485565509, "Close": 4225.4345703125, "Low": 4217.979029960617,
                        "High": 4331.431780377489},
        "2022-04-25": {"Open": 4237.487568541329, "Close": 4204.16748046875, "Low": 4171.766769167242,
                        "High": 4315.181737583676}}
'''

df = pd.DataFrame({'Date': [datetime.datetime.strptime(i, "%Y-%m-%d") for i in data.keys()],
                    'Open': [val['Open'] for key, val in data.items()],
                    'Close': [val['Close'] for key, val in data.items()], 'Low': [val['Low'] for key, val in data.items()],
                    'High': [val['High'] for key, val in data.items()]})
df = df.set_index('Date')

How to generate the same DataFrame in more effective way?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

How about:

out = pd.DataFrame.from_dict(data, orient='index').rename_axis(index='Date')
out.index = pd.to_datetime(out.index)

Output:

                   Open       Close          Low         High
Date                                                         
2022-04-22  4268.169486  4225.43457  4217.979030  4331.431780
2022-04-25  4237.487569  4204.16748  4171.766769  4315.181738
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading