简体   繁体   English

将嵌套字典转换为数据框 pandas python 的问题

[英]Issue in conversion of nested dictionary to dataframe pandas python

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.实际上,在将字典转换为数据框时,我不希望包含数字 1 和 2 的第一列。 anyone can help me out, please.任何人都可以帮助我,请。 thanks谢谢

use set_index使用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 https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.set_index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM