2

I am getting below error, AttributeError: 'DataFrame' object has no attribute 'append'. Did you mean: '_append'?

I am trying to write in result_df variable with all the device name corresponding values on each rows using for loop. But I am getting no attribute error.

what could I possibly be missing here?

Reproducible code:

import pandas as pd
import os
import json

currDir = os.getcwd()
def parse_json_response():

    filename = "my_json_file.json"
    device_name = ["Trona", "Sheldon"]
    "creating dataframe to store result"
    column_names = ["DEVICE", "STATUS", "LAST UPDATED"]
    result_df = pd.DataFrame(columns=column_names)
    my_json_file = currDir + '/' + filename

    for i in range(len(device_name)):
        my_device_name = device_name[i]
        with open(my_json_file) as f:
            data = json.load(f)

        for devices in data:
            device_types = devices['device_types']
            if my_device_name in device_types['name']:
                if device_types['name'] == my_device_name:
                    device = devices['device_types']['name']
                    last_updated = devices['devices']['last_status_update']
                    device_status = devices['devices']['status']

                    result_df = result_df.append(
                      {'DEVICE': device, 'STATUS': device_status,
                     'LAST UPDATED': last_updated}, ignore_index=True)
    print(result_df)

parse_json_response()

Here is my JSON file contents: (save in your current path named as "my_json_file.json")

[{"devices": {"id": 34815, "last_status_update": "2023-05-25 07:56:49", "status": "idle" }, "device_types": {"name": "Trona"}}, {"devices": {"id": 34815, "last_status_update": "2023-05-25 07:56:49", "status": "idle" }, "device_types": {"name": "Sheldon"}}]
3
  • You seem to be using pandas > 2.0 in which append has been removed. Try using concat or loc like df.loc[len(df)]= new_row . This solution was inspired from this. Commented May 26, 2023 at 3:20
  • can you please post as answer with my above reproducible code? @RamRattanGoyal Commented May 26, 2023 at 3:22
  • Let me give it a try. Commented May 26, 2023 at 3:22

2 Answers 2

1

This resolved,

import pandas as pd
import os
import json

currDir = os.getcwd()
def parse_json_response():

    filename = "my_json_file.json"
    device_name = ["Trona", "Sheldon"]
    "creating dataframe to store result"
    column_names = ["DEVICE", "STATUS", "LAST UPDATED"]
    result_df = pd.DataFrame(columns=column_names)
    my_json_file = currDir + '/' + filename

    for i in range(len(device_name)):
        my_device_name = device_name[i]
        with open(my_json_file) as f:
            data = json.load(f)

        for devices in data:
            device_types = devices['device_types']
            if my_device_name in device_types['name']:
                if device_types['name'] == my_device_name:
                    device = devices['device_types']['name']
                    last_updated = devices['devices']['last_status_update']
                    device_status = devices['devices']['status']

                    result_df.loc[len(result_df)] = {'DEVICE': device, 'STATUS': device_status, 'LAST UPDATED': last_updated}
    print(result_df)

parse_json_response()

Here is my JSON file contents: (save in your current path named as "my_json_file.json")

[{"devices": {"id": 34815, "last_status_update": "2023-05-25 07:56:49", "status": "idle" }, "device_types": {"name": "Trona"}}, {"devices": {"id": 34815, "last_status_update": "2023-05-25 07:56:49", "status": "idle" }, "device_types": {"name": "Sheldon"}}]
Sign up to request clarification or add additional context in comments.

Comments

1

Try this -

import pandas as pd
import os
import json

currDir = os.getcwd()
def parse_json_response():

    filename = "my_json_file.json"
    device_name = ["Trona", "Sheldon"]
    "creating dataframe to store result"
    column_names = ["DEVICE", "STATUS", "LAST UPDATED"]
    result_df = pd.DataFrame(columns=column_names)
    my_json_file = currDir + '/' + filename

    for i in range(len(device_name)):
        my_device_name = device_name[i]
        with open(my_json_file) as f:
            data = json.load(f)

        for devices in data:
            device_types = devices['device_types']
            if my_device_name in device_types['name']:
                if device_types['name'] == my_device_name:
                    device = devices['device_types']['name']
                    last_updated = devices['devices']['last_status_update']
                    device_status = devices['devices']['status']
                    
                    # One way to go.
                    result_df.loc[len(result_df)] = [device, device_status, last_updated]

                    # Another solution (better)
                    result_df = pd.concat([result_df, pd.DataFrame([{'DEVICE': device, 'STATUS': device_status,
                 'LAST UPDATED': last_updated}])], ignore_index=True)

                    # OP's implementation
                    # result_df = result_df.append(
                    #   {'DEVICE': device, 'STATUS': device_status,
                    #  'LAST UPDATED': last_updated}, ignore_index=True)
    print(result_df)

parse_json_response()

This helped me frame the solution.

9 Comments

The output should not be a list, it should be mapped to a key and value like ({'DEVICE': device, 'STATUS': device_status, 'LAST UPDATED': last_updated}, ignore_index=True)) then how?
although, when I tried result_df.loc[len(result_df)] = [device, device_status, last_updated] i am getting ValueError: cannot set a row with mismatched columns
That's weird, coz it works perfectly for me, and which output are you talking about in the first comment ?
How can we ignore the index here?
Oh great, I got another solution for you, just going to mention it as a comment on my solution, may help you in the future.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.