해결 방법: 'pandas' 모듈에 'dataframe' 속성이 없습니다.
pandas를 사용할 때 발생할 수 있는 오류는 다음과 같습니다.
AttributeError : module 'pandas' has no attribute 'dataframe'
이 오류는 일반적으로 다음 세 가지 이유 중 하나로 인해 발생합니다.
1. pd.DataFrame 대신 pd.dataframe을 작성합니다.
2. 또 다른 변수 이름은 “pd” 또는 “pandas”입니다.
3. 파일 이름은 pd.py 또는 pandas.py입니다.
다음 예에서는 이러한 각 시나리오에서 이 오류를 해결하는 방법을 보여줍니다.
이유 1: pd.dataframe 사용
다음 구문을 사용하여 pandas DataFrame을 생성한다고 가정해 보겠습니다.
import pandas as pd #attempt to create DataFrame df = pd. dataframe ({' points ': [25, 12, 15, 14], ' assists ': [5, 7, 13, 12]}) AttributeError : module 'pandas' has no attribute 'dataframe'
dataframe이라는 단어를 소문자로 썼기 때문에 오류가 발생했습니다.
Pandas DataFrame을 생성하려면 Camel Case로 ‘DataFrame’이라는 단어를 작성해야 합니다.
import pandas as pd #createDataFrame df = pd. DataFrame ({' points ': [25, 12, 15, 14], ' assists ': [5, 7, 13, 12]}) #view DataFrame df assist points 0 25 5 1 12 7 2 15 13 3 14 12
오류 없이 DataFrame을 성공적으로 생성할 수 있습니다.
이유 2: 또 다른 변수 이름은 “pd” 또는 “pandas”입니다.
스크립트의 다른 변수 이름이 “pd” 또는 “pandas”인 경우에도 이 오류가 발생할 수 있습니다.
import pandas as pd #create a list named 'pd' pd = [1, 2, 3, 4] #attempt to create DataFrame df = pd. dataframe ({' points ': [25, 12, 15, 14], ' assists ': [5, 7, 13, 12]}) AttributeError : module 'pandas' has no attribute 'dataframe'
이 오류를 해결하려면 현재 “pd”라는 변수의 이름을 다른 이름으로 바꾸면 됩니다.
import pandas as pd #create a list named 'data' data = [1, 2, 3, 4] #createDataFrame df = pd. DataFrame ({' points ': [25, 12, 15, 14], ' assists ': [5, 7, 13, 12]}) #view DataFrame df assist points 0 25 5 1 12 7 2 15 13 3 14 12
더 이상 py 또는 pandas라는 변수가 없기 때문에 오류가 발생하지 않습니다.
이유 3. 파일 이름이 pd.py 또는 pandas.py입니다.
오류가 발생할 수 있는 또 다른 이유는 스크립트의 파일 이름이 pd.py 또는 pandas.py 인 경우입니다.
이 오류를 해결하려면 파일 이름을 my_script.py , my_data.py 또는 문자 그대로 다른 이름으로 바꾸면 됩니다.
추가 리소스
다음 튜토리얼에서는 Python의 다른 일반적인 오류를 수정하는 방법을 설명합니다.
Pandas에서 KeyError를 수정하는 방법
해결 방법: ValueError: float NaN을 int로 변환할 수 없습니다.
해결 방법: ValueError: 피연산자를 모양과 함께 브로드캐스트할 수 없습니다.