1

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?

9769953
  • 10,344
  • 3
  • 26
  • 37
Sriram
  • 169
  • 1
  • 9

1 Answers1

0

You can try-

b = data_df.to_dict(orient='index')
Dharman
  • 30,962
  • 25
  • 85
  • 135