Error

[Python, pandas] AttributeError: 'DataFrame' object has no attribute 'append'

_Sun_ 2023. 6. 27. 13:40

비어 있는 데이터 프레임에 컬럼들을 append를 하려다가 발생한 오류.

 

사용한 pandas 라이브러리 version은 2.0.2이다.

 

>> import pandas as pd
>> pd.__version__

'2.0.2'

 

 

 

찾아보니 pandas 1.5 버전까지는 append 속성이 있었으나, pandas 2.0부터 append 속성이 제거된 것으로 보인다. 

 

pandas 2.0 이상 버전을 사용하는 경우, append 대신 concat 함수를 이용해야 할 듯.

 

 

 

 

 

Concat 함수로 오류 해결하기


1. 추가하려는 데이터를 DataFrame으로 만들고,

2. 앞서 만든 DataFrame과 concat

 

import pandas as pd

food_df = pd.DataFrame()
data = {"NUM":"", "FOOD_CD":"","SAMPLING_REGION_NAME":"","SAMPLING_MONTH_NAME":"","SAMPLING_REGION_CD":"",
        "SAMPLING_MONTH_CD":"", "GROUP_NANE":"", "DESC_KOR":"", "SERVING_SIZE":"", "NUTR_CONT1":"", 
        "NUTR_CONT2":"", "NUTR_CONT3":"", "NUTR_CONT4":"", "NUTR_CONT5":"", "NUTR_CONT6":"", 
        "NUTR_CONT7":"", "NUTR_CONT8":"", "NUTR_CONT9":""}
food_df = pd.concat([food_df, pd.DataFrame([data])], ignore_index=True)

 

 

그러면 오류가 해결되어 원래 의도한 컬럼만 있는 데이터프레임이 도출되는 걸 확인할 수 있다.