0

I am writing multiple rows from a for loop writing these columns,

for i in data:
    result_df = result_df.append({'DEVICE': 'devicename1', 'POOL': 'poolname1', 'DSN': '1234x',
                             'STATUS': 'online', 'LAST UPDATED': 'date',
                             'RUNNING TIME': '1', 'CONNECTED HOST': 'somehost',
                             'HW_CONFIG': 'conf', 'USER': 'user_id1'}, ignore_index=True)
print(result_df)

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

What am i missing here? can someone show me?

EDIT 2: with reproducible codes,

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"}}]
iltech
  • 183
  • 1
  • 2
  • 11
  • `append` was removed in the last pandas version. Anyway, you should never use `append`/`concat` in a loop. Collect the data in a list and `concat` **once** in the end – mozway May 25 '23 at 08:49
  • can you let me know more about it with my code sample? – iltech May 25 '23 at 09:07
  • Hard to say for sure as your example is not reproducible and the loop seems useless here, but something along the lines of: `lst = [] ; for i in data: lst.append(…) ; result_df = pd.concat([result_df, pd.DataFrame(lst)])` – mozway May 25 '23 at 09:10
  • Also since it is a Dictionary, I cant use append. And since it is a list i cant use append as it has key value pair. @mozway – iltech May 25 '23 at 09:15
  • Then please provide your full reproducible code, data and expected output – mozway May 25 '23 at 09:19
  • can we chat? if its ok – iltech May 25 '23 at 09:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/253818/discussion-between-iltech-and-mozway). – iltech May 25 '23 at 09:23

0 Answers0