Dictionary of dictionaries from data frame

91 Views Asked by At

I have created a data frame from data got from database. I need to convert the data frame to dictionary with format mentioned below.

{ 0 : {'column1': 'value', 'column2' : 'value',....},
1 : {'column1': 'value', 'column2' : 'value',....},....

I tried

data_list = [tuple(r) for r in data_df.to_numpy().tolist()]#convert df to list of tuples

data_dict = [dict(zip(ag_titles, x)) for x in data_list]
data_final = {i:{k:v for k,v in dict(data_dict).items}for i in range(len(data_dict))}

But I am not getting the expected output. How can this be done with dictionary comprehension?

1

There are 1 best solutions below

1
Sarthak Sinha On

You can try-

b = data_df.to_dict(orient='index')