如果Python中不存在For循环迭代,则获取空值

时间:2019-06-21 08:37:40

标签: python json pandas csv dataframe

我只有几个静态键列EmployeeId,type和几个列来自第一个FOR循环。

在第二个FOR循环中,如果我有特定的键,则仅应将值附加到现有的数据帧列上,否则从第一个for循环中获取的列应保持不变。

第一个循环输出

"EmployeeId","type","KeyColumn","Start","End","Country","Target","CountryId","TargetId"
"Emp1","Metal","1212121212","2000-06-17","9999-12-31","","","",""

在“第二次循环”之后,我的输出如下:

"EmployeeId","type","KeyColumn","Start","End","Country","Target","CountryId","TargetId"
"Emp1","Metal","1212121212","2000-06-17","9999-12-31","","AMAZON","1",""
"Emp1","Metal","1212121212","2000-06-17","9999-12-31","","FLIPKART","2",""

根据代码,如果我有Employee标签可用,我有2条以上的记录,但是我可能有一些没有Employee标签的json文件,则输出应与First Loop Output相同。

但是根据我的代码,我得到了0条记录。如果我的编码方式有误,请帮助我。

真的很抱歉-如果询问方式不清楚,因为我是python的新手。请在下面的超级链接中找到代码

请找到以下代码

    for i in range(len(json_file['enty'])):
        temp = {}
        temp['EmployeeId'] = json_file['enty'][i]['id']
        temp['type'] = json_file['enty'][i]['type']
        for key in json_file['enty'][i]['data']['attributes'].keys(): 
            if(key == 'Employee'):
                for j in range(len(json_file['enty'][i]['data']['attributes']['Employee']['group'])):
                    for key in json_file['enty'][i]['data']['attributes']['Employee']['group'][j].keys():
                        try:
                            temp[key] = json_file['enty'][i]['data']['attributes']['Employee']['group'][j][key]['values'][0]['value']
                        except:
                            temp[key] = None

                    temp_df = pd.DataFrame([temp])
                    df = pd.concat([df, temp_df], sort=True)

如果雇员标签不可用,我将获得0条记录作为输出,但我希望有1条记录作为第一个for循环

enter link description here

0 个答案:

没有答案