Home > Net >  Issue in conversion of nested dictionary to dataframe pandas python
Issue in conversion of nested dictionary to dataframe pandas python

Time:07-25

I have data in form of nested dictionary as below

    data = {
        "policy": {
            "1": {
                "ID": "ML_0",
                "URL": "www.a.com",
                "Text": "my name is Martin and here is my code"
            },
            "2": {
                "ID": "ML_1",
                "URL": "www.b.com",
                "Text": "my name is Mikal and here is my code"
            }
        }
    }

I'm trying to convert it in a kind of dataframe by using the below code.

for policies in data['policy']:

  new = pd.DataFrame.from_dict(data['policy'], orient='index')

and I get this


    ID  URL Text
1   ML_0    www.a.com   my name is Martin and here is my code
2   ML_1    www.b.com   my name is Mikal and here is my code

Actually I don't want the first column which includes numbers 1 and 2 when converting my dictionary to dataframe. anyone can help me out, please. thanks

CodePudding user response:

use set_index

for policies in data['policy']:
    new = pd.DataFrame.from_dict(data['policy'], orient='index').set_index('ID')

Refer below documentation for more information and options

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.set_index.html

  • Related