How to append a dictionary to a pandas dataframe?

Blue Moon

I have a set of urls containing json files and an empty pandas dataframe with columns representing the attributes of the jsnon files. Not all json files have all the attributes in the pandas dataframe. What I need to do is to create dictionaries out of the json files and then append each dictionary to the pandas dataframe as a new row and, in case the json file doesn't have an attribute matching a column in the dataframe this has to be filled blank.

I managed to create dictionaries as:

import urllib2
import json  

url = "https://cws01.worldstores.co.uk/api/product.php?product_sku=ULST:7BIS01CF"
data = urllib2.urlopen(url).read()
data = json.loads(data)

and then I tried to create a for loop as follows:

row = -1
for i in links:
    row = row + 1
    data = urllib2.urlopen(str(i)).read()
    data = json.loads(data)
    for key in data.keys():
        for column in df.columns:
            if str(column) == str(key):
                df.loc[[str(column)],row] = data[str(key)]
            else:
                df.loc[[str(column)],row] = None

where df is the dataframe and links is the set of urls

However, I get the following error:

raise KeyError('%s not in index' % objarr[mask])

KeyError: "['2_seater_depth_mm'] not in index"

where ['2_seater_depth_mm'] is the first column of the pandas dataframe

zuku

For me below code works:

row = -1
for i in links:
    row = row + 1
    data = urllib2.urlopen(str(i)).read()
    data = json.loads(data)
    for key in data.keys():
        df.loc[row,key] = data[key]

You have mixed order of arguments in .loc() and have one to much []

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Append Dictionary Elements into an Empty Pandas Dataframe Column

From Java

How to append rows in a pandas dataframe in a for loop?

From Dev

Python + Pandas + Spark - How to import a dataframe into Pandas dataframe and convert it into a dictionary?

From Java

How to convert dataframe to dictionary in pandas WITHOUT index

From Java

How to convert pandas dataframe to hierarchical dictionary

From Dev

How to convert pandas dataframe to hierarchical dictionary

From Dev

How to convert dataframe to dictionary in pandas WITHOUT index

From Java

Append column to pandas dataframe

From Dev

Append DataFrame to an Index Pandas

From Dev

Append to a dataframe in Pandas

From Dev

If and append condition pandas dataframe?

From Dev

Python Pandas append dataframe

From Dev

open all csv in a folder one by one and append them to a dictionary or pandas dataframe

From Dev

open all csv in a folder one by one and append them to a dictionary or pandas dataframe

From Java

Convert a Pandas DataFrame to a dictionary

From Dev

pandas dataframe to dictionary value

From Dev

Dictionary of Pandas' Dataframe to JSON

From Dev

Python Dictionary to Pandas Dataframe

From Dev

Pandas dataframe to dictionary of lists

From Dev

dictionary inside of a pandas dataframe

From Dev

Convert Dictionary to Pandas DataFrame

From Dev

Dictionary from Pandas dataframe

From Dev

Pandas dataframe to nested dictionary

From Dev

How to append data to pandas multi-index dataframe

From Dev

How to append selected columns to pandas dataframe from df with different columns

From Dev

How can we append unbalanced row on Pandas dataframe in the fastest way?

From Dev

How to append two pandas.DataFrame with different numbers of columns

From Dev

Append values to an empty PANDAS dataframe

From Dev

Append pandas DataFrame column to CSV

Related Related

  1. 1

    Append Dictionary Elements into an Empty Pandas Dataframe Column

  2. 2

    How to append rows in a pandas dataframe in a for loop?

  3. 3

    Python + Pandas + Spark - How to import a dataframe into Pandas dataframe and convert it into a dictionary?

  4. 4

    How to convert dataframe to dictionary in pandas WITHOUT index

  5. 5

    How to convert pandas dataframe to hierarchical dictionary

  6. 6

    How to convert pandas dataframe to hierarchical dictionary

  7. 7

    How to convert dataframe to dictionary in pandas WITHOUT index

  8. 8

    Append column to pandas dataframe

  9. 9

    Append DataFrame to an Index Pandas

  10. 10

    Append to a dataframe in Pandas

  11. 11

    If and append condition pandas dataframe?

  12. 12

    Python Pandas append dataframe

  13. 13

    open all csv in a folder one by one and append them to a dictionary or pandas dataframe

  14. 14

    open all csv in a folder one by one and append them to a dictionary or pandas dataframe

  15. 15

    Convert a Pandas DataFrame to a dictionary

  16. 16

    pandas dataframe to dictionary value

  17. 17

    Dictionary of Pandas' Dataframe to JSON

  18. 18

    Python Dictionary to Pandas Dataframe

  19. 19

    Pandas dataframe to dictionary of lists

  20. 20

    dictionary inside of a pandas dataframe

  21. 21

    Convert Dictionary to Pandas DataFrame

  22. 22

    Dictionary from Pandas dataframe

  23. 23

    Pandas dataframe to nested dictionary

  24. 24

    How to append data to pandas multi-index dataframe

  25. 25

    How to append selected columns to pandas dataframe from df with different columns

  26. 26

    How can we append unbalanced row on Pandas dataframe in the fastest way?

  27. 27

    How to append two pandas.DataFrame with different numbers of columns

  28. 28

    Append values to an empty PANDAS dataframe

  29. 29

    Append pandas DataFrame column to CSV

HotTag

Archive