Please tell me the solution to the problem: I have the following list, obtained through json:

'Sides': [{ 'ShortName': 'АО "Компания 1', 'Type': 0, 'Inn': '7777777771', 'Ogrn': '1111111111111', }, {'ShortName': 'ООО "Компания 2', 'Type': 1, 'Inn': '77777777772', 'Ogrn': '55555555555'}] 

The goal is to write a function that will transfer data to DataFrame according to Type: i.e. if Type 0, then data from the corresponding dictionary is taken.

 def Paintiff(Sides): if len(Sides) > 0: for data in Sides: if data['Type'] == 0: return data['ShortName'], data['Inn'], data['Ogrn'] return None for company in companies: c = { 'ShortName_Plantiff': Paintiff(company['Sides']), 'ShortName_Defendant': Paintiff(company['Sides']) 'Inn': Paintiff(company['Sides'] 'ogrn': Paintiff(company['Sides'] } df_raw = df_raw.append(c, ignore_index=True) 

I tried to write the code (above), but it turns out that he takes only the first name of the company and nothing more :(

    1 answer 1

    The easiest way is to create a DataFrame from all data and filter it on the fly:

     In [380]: df = pd.DataFrame(Sides).query("Type == 0") In [381]: df Out[381]: Inn Ogrn ShortName Type 0 7777777771 1111111111111 АО "Компания 1 0 

    You can also filter the list of dictionaries before creating the DataFrame:

     In [382]: pd.DataFrame(x for x in Sides if x['Type'] == 0) Out[382]: Inn Ogrn ShortName Type 0 7777777771 1111111111111 АО "Компания 1 0