Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中从数据帧到嵌套字典_Python_Python 3.x_Pandas - Fatal编程技术网

Python中从数据帧到嵌套字典

Python中从数据帧到嵌套字典,python,python-3.x,pandas,Python,Python 3.x,Pandas,我想转换为特定的嵌套字典格式。 数据帧df如下所示: OC OZ ON WT DC DZ DN 0 PL 97 TP 59 DE 63 DC 3 US 61 SU 9 US 95 SU 预期输出如下: {'location': { 'zipCode': {'country': 'PL', 'code': '97'}, 'location':

我想转换为特定的嵌套字典格式。 数据帧df如下所示:

    OC  OZ  ON  WT  DC  DZ  DN
0   PL  97  TP  59  DE  63  DC 
3   US  61  SU  9   US  95  SU
预期输出如下:

{'location': 
        {
        'zipCode': 
            {'country': 'PL',
            'code': '97'},
        'location': {'id': '1'}, 
        'longName': 'TP',  
            },
    'CarriageParameter': 
         {'road': 
              {'truckLoad': 'Auto'}
            },
    'load': 
         {'weight': '59',
          'unit': 'ton',
          'showEmissionsAtResponse': 'true'
         }
    },
    {'location': 
        {
        'zipCode': 
            {'country': 'DE',
            'code': '63'},
        'location': {'id': '2'}, 
        'longName': 'DC']
        },
        'CarriageParameter':
             {'road': 
                 {'truckLoad': 'Auto'}
            },      
        'unload': {
            'weight': '59'),
            'unit': 'ton',
            'showEmissionsAtResponse': 'true'
        }
}
我尝试了下面的代码,但只得到字典的一部分:

dic = {}
dic['section'] = []
for ix, row in df.iterrows():
    in_dict1 = {
    'location': 
        {
        'zipCode': 
            {'country': row['OC'],
            'code': row['OZ']},
        'location': {'id': '1'}, 
        'longName': row['ON'],  
            },
    'CarriageParameter': 
         {'road': 
              {'truckLoad': 'Auto'}
            },
    'load': 
         {'weight': str(row['WT']),
          'unit': 'ton',
          'showEmissionsAtResponse': 'true'
         }
    },
    in_dict2 = {'location': 
        {
        'zipCode': 
            {'country': row['DC'],
            'code': row['DZ']},
        'location': {'id': '2'}, 
        'longName': row['DN']
        },
        'CarriageParameter':
             {'road': 
                 {'truckLoad': 'Auto'}
            },      
        'unload': {
            'weight': str(row['WT']),
            'unit': 'ton',
            'showEmissionsAtResponse': 'true'
        }
        }
    dic['section'].append(in_dict1)
下面第一行的漂亮字体:

{'section': [{'CarriageParameter': {'road': {'truckLoad': 'Auto'}},
              'load': {'showEmissionsAtResponse': 'true',
                       'unit': 'ton',
                       'weight': '59'},
              'location': {'location': {'id': '1'},
                           'longName': 'TP COLEP_GRABICA PL',
                           'zipCode': {'code': '97-306', 'country': 'PL'}}}]}
我希望字典的第二部分在某个地方丢失了


如何解决此问题?

因为您没有添加第二个词典,您可以尝试以下操作:

import pandas as pd
import io

s_e='''
    OC  OZ  ON  WT  DC  DZ  DN
0   PL  97  TP  59  DE  63  DC 
3   US  61  SU  9   US  95  SU
'''
df = pd.read_csv(io.StringIO(s_e), sep='\s\s+', parse_dates=[1,2], engine='python')

dic = {}
dic['section'] = []
for ix, row in df.iterrows():
    in_dict1 = {
    'location': 
        {
        'zipCode': 
            {'country': row['OC'],
            'code': row['OZ']},
        'location': {'id': '1'}, 
        'longName': row['ON'],  
            },
    'CarriageParameter': 
         {'road': 
              {'truckLoad': 'Auto'}
            },
    'load': 
         {'weight': str(row['WT']),
          'unit': 'ton',
          'showEmissionsAtResponse': 'true'
         }
    }
    in_dict2 = {'location': 
        {
        'zipCode': 
            {'country': row['DC'],
            'code': row['DZ']},
        'location': {'id': '2'}, 
        'longName': row['DN']
        },
        'CarriageParameter':
             {'road': 
                 {'truckLoad': 'Auto'}
            },      
        'unload': {
            'weight': str(row['WT']),
            'unit': 'ton',
            'showEmissionsAtResponse': 'true'
        }
        }
    dic['section'].append(in_dict1)
    dic['section'].append(in_dict2)
    
print(dic['section'])
输出:

[{'location': {'zipCode': {'country': 'PL', 'code': 97}, 'location': {'id': '1'}, 'longName': 'TP'}, 'CarriageParameter': {'road': {'truckLoad': 'Auto'}}, 'load': {'weight': '59', 'unit': 'ton', 'showEmissionsAtResponse': 'true'}}, {'location': {'zipCode': {'country': 'DE', 'code': 63}, 'location': {'id': '2'}, 'longName': 'DC'}, 'CarriageParameter': {'road': {'truckLoad': 'Auto'}}, 'unload': {'weight': '59', 'unit': 'ton', 'showEmissionsAtResponse': 'true'}}, {'location': {'zipCode': {'country': 'US', 'code': 61}, 'location': {'id': '1'}, 'longName': 'SU'}, 'CarriageParameter': {'road': {'truckLoad': 'Auto'}}, 'load': {'weight': '9', 'unit': 'ton', 'showEmissionsAtResponse': 'true'}}, {'location': {'zipCode': {'country': 'US', 'code': 95}, 'location': {'id': '2'}, 'longName': 'SU'}, 'CarriageParameter': {'road': {'truckLoad': 'Auto'}}, 'unload': {'weight': '9', 'unit': 'ton', 'showEmissionsAtResponse': 'true'}}]

看起来您没有在列表中添加
。\u dict2
感谢您的回答,但它会继续添加上一行。我怎样才能解决这个问题?