Convert Dataframe to Nested Dictionary in Python
问题 I’m looking for a way to convert a dataframe into a dictionary, very similar to what has been asked here: Convert pandas DataFrame to a nested dict Assuming a sample data frame name v1 v2 v3 0 A A1 A11 1 1 A A2 A12 2 2 B B1 B12 3 3 C C1 C11 4 4 A A2 A21 6 5 A A2 A21 8 The number of columns may differ and so does the column names. I’m looking to generate : { 'A' : { 'A1' : { 'A11' : 1 }, 'A2' : { 'A12' : 2 , 'A21' : 6 , 'A21' : 8 }, 'B1' : {}, 'C1' : {} }, 'B' : { 'A1' : {}, 'A2' : {}, 'B1' :