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

Load the python dictionary from csv file

I saved the dictionary dict dict_keys(['df1', 'df2']) into a csv file. cat data.csv outputs something like this?

,,df1,df2
quantile_10,h0,0.03636844456195831,0.105098
quantile_10,h5,0.0654495671391487,0.108322
quantile_10,h10,0.10933497846126557,0.113496
quantile_10,h15,0.1400846481323242,0.119098
quantile_10,h20,0.1513956755399704,0.068324
quantile_10,h25,0.11640003025531769,0.017974
quantile_10,h30,0.026758757233619694,9.4e-05
quantile_10,h35,0.0020915842149406673,0.0
quantile_20,h0,0.05211468040943147,0.130436
quantile_20,h5,0.08270514607429505,0.125416
quantile_20,h10,0.12569279968738556,0.125436
quantile_20,h15,0.14520362317562102,0.149596

How to open this dictionary in python so I have a dict['df1'], dict['df2'] and quantiles being row indexes?

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 :

You can use the pandas library to read this CSV file and reconstruct the original dictionary.

  • Read the csv with the first two columns set as the index
  • Convert the DataFrame to a dictionary where keys are column names (df1, df2, etc.) and values are DataFrames with the multi-index intact.
import pandas as pd

# Read the CSV with the first two columns as index
df = pd.read_csv('data.csv', index_col=[0, 1])

# Extract each 'df' as a transposed DataFrame and store in a dictionary
data_dict = {col: df[[col]].unstack().T for col in df.columns}

# Access the data
print(data_dict['df1'].columns)
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