Home > Enterprise >  How to convert each row/cell values from a DataFrame to a list of dictionaries in pandas?
How to convert each row/cell values from a DataFrame to a list of dictionaries in pandas?

Time:12-16

I have a pandas DataFrame below:

df_input = pd.DataFrame({
         'Domain':['www.google.com','www.apple.com','www.amazon.com'],
         'Description':['The company’s product portfolio includes Googl...','Apple is a multinational corporation that desi...','Amazon is an international e-commerce website ...'],
         'About Us':['Google is a multinational corporation that spe...','Apple is a multinational corporation that desi...','Amazon is an e-commerce website for consumers,...'],
         'Founded':[1998, 1976, 1994],
         'Country':['United States','United States','United States']})

enter image description here

I want to transfer it as the following:

[{"properties": [{"name": "Description","value": "The company’s product portfolio includes Google Search, which provides users with access to information online; Knowledge Graph that allows users to search for things, people, or places as well as builds systems recognizing speech and understanding"},
            {"name": "Domain","value": "www.google.com"},
            {"name": "About Us", "value": "Google is a multinational corporation that specializes in Internet-related services and products."},
            {"name": "Founded", "value": 1998},
            {"name": "Country", "value":"United States"}]},

{"properties": [{"name": "Description","value": "Apple is a multinational corporation that designs, manufactures, and markets mobile communication and media devices, personal computers, portable digital music players, and sells a variety of related software, services, peripherals, networking solutions, and third-party digital content and applications."},
            {"name": "Domain","value": "www.apple.com"},
            {"name": "About Us", "value": "Apple is a multinational corporation that designs, manufactures, and markets consumer electronics, personal computers, and software."},
            {"name": "Founded", "value": 1976},
            {"name": "Country", "value":"United States"}]},

{"properties": [{"name": "Description","value": "Amazon is an international e-commerce website for consumers, sellers, and content creators. It offers users merchandise and content purchased for resale from vendors and those offered by third-party sellers."},
            {"name": "Domain","value": "www.amazon.com"},
            {"name": "About Us", "value": "Amazon is an e-commerce website for consumers, sellers, and content creators."},
            {"name": "Founded", "value": 1994},
            {"name": "Country", "value":"United States"}]}]

How can I write a loop to do this?

CodePudding user response:

You just iterate through the rows.

import pandas as pd

df_input = pd.DataFrame({
         'Domain':['www.google.com','www.apple.com','www.amazon.com'],
         'Description':['The companys product portfolio includes Googl...','Apple is a multinational corporation that desi...','Amazon is an international e-commerce website ...'],
         'About Us':['Google is a multinational corporation that spe...','Apple is a multinational corporation that desi...','Amazon is an e-commerce website for consumers,...'],
         'Founded':[1998, 1976, 1994],
         'Country':['United States','United States','United States']})

data = []
for row in df_input.iterrows():
    props = []
    for key,val in zip(df_input.columns, row[1].values ):
        props.append( {'name':key, 'value':val} )
    data.append( {'properties': props} )
from pprint import pprint
pprint(data)

Output:

[{'properties': [{'name': 'Domain', 'value': 'www.google.com'},
                 {'name': 'Description',
                  'value': 'The companys product portfolio includes Googl...'},
                 {'name': 'About Us',
                  'value': 'Google is a multinational corporation that spe...'},
                 {'name': 'Founded', 'value': 1998},
                 {'name': 'Country', 'value': 'United States'}]},
 {'properties': [{'name': 'Domain', 'value': 'www.apple.com'},
                 {'name': 'Description',
                  'value': 'Apple is a multinational corporation that desi...'},
                 {'name': 'About Us',
                  'value': 'Apple is a multinational corporation that desi...'},
                 {'name': 'Founded', 'value': 1976},
                 {'name': 'Country', 'value': 'United States'}]},
 {'properties': [{'name': 'Domain', 'value': 'www.amazon.com'},
                 {'name': 'Description',
                  'value': 'Amazon is an international e-commerce website ...'},
                 {'name': 'About Us',
                  'value': 'Amazon is an e-commerce website for consumers,...'},
                 {'name': 'Founded', 'value': 1994},
                 {'name': 'Country', 'value': 'United States'}]}]
  • Related