How to convert rows in df to dictionary?

Viewed 9

Suppose I have the following df,

Column1 | Column2 | Column3
1       |    4    | 23.2
32      |    4.2  | 62.2
9       |    12   | 2.2

I want to be able to get dictionary in the following format,

{
    0: {'Column1':1, 'Column2':4, 'Column3':23.2},
    1: {'Column1': 32, 'Column2':4.2, 'Column3':62.2},
    2: {'Column1':9, 'Column2':12, 'Column3':2.2}
}

How can I achieve this?

1 Answers

final_dict = df.set_index(df.index).T.to_dict('dict')

Related