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"}}]
ilexcel
  • 895
  • 8
  • 12
  • You seem to be using pandas > 2.0 in which append has been removed. Try using [concat](https://pandas.pydata.org/docs/reference/api/pandas.concat.html) or [loc](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html) like `df.loc[len(df)]= new_row` . This solution was inspired from [this](https://stackoverflow.com/a/75956237/19060733). – rr_goyal May 26 '23 at 03:20
  • can you please post as answer with my above reproducible code? @RamRattanGoyal – ilexcel May 26 '23 at 03:22
  • Let me give it a try. – rr_goyal May 26 '23 at 03:22

2 Answers2

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.

rr_goyal
  • 467
  • 2
  • 8
  • 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? – ilexcel May 26 '23 at 03:30
  • 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` – ilexcel May 26 '23 at 03:33
  • That's weird, coz it works perfectly for me, and which output are you talking about in the first comment ? – rr_goyal May 26 '23 at 03:37
  • How can we ignore the index here? – ilexcel May 26 '23 at 03:39
  • 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. – rr_goyal May 26 '23 at 03:40
  • yes please, also how can we ignore index? – ilexcel May 26 '23 at 03:41
  • U are using the ignore_index param, setting that to true does the job, however [this](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.reset_index.html) function might also come in handy. – rr_goyal May 26 '23 at 03:45
  • I am not sure how to set ignore index to true here. can you please assist? – ilexcel May 26 '23 at 03:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/253835/discussion-between-ilexcel-and-rr-goyal). – ilexcel May 26 '23 at 06:31
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"}}]
ilexcel
  • 895
  • 8
  • 12