Extract Index from a list of dictionaries [closed]

I have a dataframe which is like below

   ID       Name        Age
   101      Bart         10 
   234      Homer        39

I convert it to a dictionary using code -:

    dic1 = df6.to_dict(orient = 'index')

For Example -:

      {101:{'name':'Bart', 'age':10}, 234:{'name':'Homer', 'age':39}}

But I want except the first column every thing else should convert to a dictionary .

      ID    list
      101    {'name':'Bart', 'age':10} 
      234    {'name':'Homer', 'age':39}

Answer

You can use a list comprehension with the .keys() and .values() of a dictionary:

import pandas as pd

df = pd.DataFrame({"ID": [101, 234], "Name": ["Bart", "Homer"], "Age": [10, 39]})
records = df.set_index("ID").to_dict(orient="index")
pd.DataFrame([[v] for v in records.values()], index=records.keys())

which prints

                                0
101   {'Name': 'Bart', 'Age': 10}
234  {'Name': 'Homer', 'Age': 39}