0

我有多个如下所示的有序字典:

OrderedDict([('Record ID',
              {'Base Data Type': 'Int',
               'SQL Name': 'RecordId',
               'MySQL Type': 'INTEGER',
               'PostgreSQL Type': 'INTEGER'})])

我该如何去改变它:

OrderedDict([('tblProviderDetails',
              OrderedDict([('Record ID',
                            {'Base Data Type': 'Int',
                             'SQL Name': 'RecordId',
                             'MySQL Type': 'INTEGER',
                             'PostgreSQL Type': 'INTEGER'})]))])

这是我用来创建有序字典的代码,但我希望能够用我想要的任何东西嵌套它们,在这种情况下,它是“tblProviderDetails”。注意:我在上面的字典函数中使用的 key_index 被称为“数据元素名称”,它是数据帧中第 1 列的名称。

def create_dict(df, key_index='uniqueID'):
    """
    Function create_dict to take input pandas df and return ordered dictionary

    args:
        df: pandas 2-dim labeled data structure
        key_index: the column that we want set as the index
    
    input: pandas df
    returns: dictionary

    """
    # Get the unordered dictionary
    unordered_dict = df.set_index(key_index).T.to_dict()
    
    # Order the dictionary
    ordered_dict = OrderedDict((k,unordered_dict.get(k)) for k in df[key_index])
    
    return ordered_dict

谢谢!

4

0 回答 0