'DataFrame' object has no attribute 'append'

H Lee·2023년 10월 3일
0

KT AICE Associate 실습을 진행하던 도중 아래의 오류가 발생하였다.

문제

for i in np.arange(len(json_data)):
    if json_data[i].json()['documents'] != []:
        address_data = address_data.append(
            [(json_data[i].json()['documents'][0]['road_address']['address_name'],
              json_data[i].json()['documents'][0]['road_address']['x'],
              json_data[i].json()['documents'][0]['road_address']['y'])],
            ignore_index=True)
                                           
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
AttributeError: 'DataFrame' object has no attribute 'append'
  • 'DataFrame' object has no attribute 'append'

해결법

https://stackoverflow.com/questions/75956209/error-dataframe-object-has-no-attribute-append

pandas 2.0, append (previously deprecated) was removed.
You need to use concat instead

수정한 코드

for i in np.arange(len(json_data)):
    if json_data[i].json()['documents'] != []:
        address_name = json_data[i].json(
        )['documents'][0]['road_address']['address_name']
        x = json_data[i].json()['documents'][0]['road_address']['x']
        y = json_data[i].json()['documents'][0]['road_address']['y']

        new_data = pd.DataFrame(
            {'address_name': [address_name], 'x': [x], 'y': [y]})
        
        address_data = pd.concat([address_data, new_data], ignore_index=True)
profile
메모

0개의 댓글