inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

파이썬 기초 라이브러리부터 쌓아가는 머신러닝

DataFrame에서 자주 사용하는 전처리 기법

11분 55초 오류

116

김동현

작성한 질문수 1

0

11분 55초에서

nan_abalone_df.mean()

를 입력하면 아래와 같은 오류가 발생합니다. 선생님과 코드가 모두 동일한데 . 왜이럴까요.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-105-677ba292c7ae> in <cell line: 0>()
----> 1 nan_abalone_df.mean()

10 frames
/usr/local/lib/python3.11/dist-packages/pandas/core/nanops.py in _ensure_numeric(x)
   1684             if inferred in ["string", "mixed"]:
   1685                 # GH#44008, GH#36703 avoid casting e.g. strings to numeric
-> 1686                 raise TypeError(f"Could not convert {x} to numeric")
   1687             try:
   1688                 x = x.astype(np.complex128)

TypeError: Could not convert ['MMFMIIFFMFFMMFFMIFMMMIFFFFFMMMMFMFFMFFFMFFIIIIMFIFIMMFMFMMIFMMFMMMFFFIMFFMFFMFFFFMFMMFMMFFMMMFMMMMMFIMMMMFFFFFMMIMFFFMFMFIFMIIIIMMMFFIIFFMFMFFMMMFMIIIMFFFFMFMFFMFMFFMFFMFMFMFIIIIIMMMFFFMFFFFMMMIFMFMMMFMFFMFIIFIFMFMFMMIMFFFFFIFFIFFMMMIIIIIIIMIIIIIIIIIIMFFMMMMFFFFMIMFMMMFFMMMMMFMMFFIFMMFMFMFMFMFMIIIMMFFMMFIIMMMFMMFMIFMFIIIMIMIIMMIMFIIFMMMMFMFMFFMFIIMFFMMMMMFMFMFFFFMMFMFFFFFMFMFMFMMMMMFMMMIIMFIIIFMMMFMFFIFMMMMMMFMFFFMFMFIFIIMFMFFFMMMIMIIMMIIFIFFMMMFMFFFFMFIIFMFIIIIFMFMFFIFFMFMMMFMMFMFMFMFMMFMMFFFFMFFFFFMMMMIIFFMFMMMMFMFIMMMMFFMIFIFIIIMMFFFMMFMMMFIIIIIIIIFFIFIFFFMIIMFIIFFIFIFIFFIFIFIIFMFIFIFMIFIIFIFFIIIMIMMFMMIIMFMMMMIFFIFFFIMIFMIMMMMIFMIFMMIMMIFMIIMFMFFIFIIFFIFMFMFMFFMFFMFFFIMFMMMFFMMMIFMIIIMFFMFFMMMMFMIIMIMMIIIIIIMMMMFMFMFMMMMMMMFMMMFFFMFFMMMFMFFMMMFMMMFFMFMFMFFFIMMMMMMMFMMMIIMFMFFFFMMMMMMMFMMMIMMIFFFMFIIIIIIIIIIIIIIIIIIMIIIIIMIFIMFMMMMFFFMFMMMMFMMFFFFMFMFMFFMMFMFFMMFFFMMMMMFFFFFFMIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFIIIIIIIIIIIIFMFMFMMMMFMMMMIMMFIMIMIIMMFMFMFFMFMMFFFFMMFMMMFFFMFMMMFFFMFMFMMMMFFFMFFFMFMMMMFFFMMFMFFMMMFFFMMFMFFMMMFFMMFFMIIIIIIIIIIIIIIIIIIIIIIIIIIIMIIIIIIIIIIIIMIIIIMIIIMFIFIIIMIIFIIMIIIIMMMFFMMMFMMMFMFIFFMFMIMMMMMMMFMMMFMFMFMMFMMMMFFMMFFMMMFMMFFMMFFMIFFMFMFMMMFFFMMFFMMFMMFFMFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFIIIFIIIIIIIIIIIIMIMIIIIIIIIIIIIIMIIIIIIFMFIIFIIIFMIIMIIIIIFIIFMMIIFMIMFFMMMFMMIMMMMIMMFFMMIFFMIMIFFMMMIMMFMMFMMFMMFFMFFFFIMIMMFFMMMMMMMFFMMFFMFMFFFFFMMFFMMFFFMFFFFFFIIIIIIIIIIIIIIMIIIMIMIIIII...
 'length_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_longlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_longlength_longlength_longlength_longlength_longlength_longlength_longlength_longlength_longlength_longlength_longlength_longlength_longlength_shortlength_shortlength_shortlength_longlength_shortlength_shortlength_longlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_longlength_longlength_shortlength_shortlength_longlength_shortlength_longlength_longlength_longlength_longlength_longlength_longlength_longlength_longlength_longlength_longlength_shortlength_longlength_longlength_longlength_longlength_longlength_shortlength_longlength_longlength_longlength_longlength_longlength_longlength_longlength_shortlength_shortlength_shortlength_shortlength_shortlength_longlength_shortlength_shortlength_longlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_longlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlength_shortlen...

머신러닝 pandas kaggle scikit-learn matplotlib

답변 1

0

인프런 AI 인턴

안녕하세요, 인프런 AI 인턴입니다. 김동현 님.

문의하신 내용은 pandas 라이브러리에서 데이터프레임을 다룰 때 자주 발생할 수 있는 오류입니다. TypeError: Could not convert...라는 오류 메시지는 mean() 함수를 호출할 때, 데이터프레임의 특정 열에 문자열 데이터가 포함되어 있어 발생합니다. pandasmean() 함수는 숫자 데이터에 대해서만 평균을 계산할 수 있기 때문에, 문자열 데이터가 포함되어 있으면 에러가 발생하게 됩니다.

이 문제를 해결하려면, mean() 함수를 호출하기 전에 문자열이 포함된 열을 제거하거나, 숫자 데이터만 선택하여 계산해야 합니다. 예를 들어, 다음과 같은 방법을 사용할 수 있습니다:

  1. 데이터프레임에서 문자열 컬럼 제거 (또는 숫자형 컬럼만 선택)

    numeric_df = nan_abalone_df.select_dtypes(include=[np.number])
    mean_values = numeric_df.mean()
    
  2. 특정 컬럼만 지정하여 평균값 계산
    만약 특정 컬럼에 대해서만 평균 값을 계산하고 싶다면, 해당 컬럼만 선택해서 mean()을 호출할 수 있습니다:

    mean_value = nan_abalone_df['numeric_column'].mean()
    

관련하여 비슷한 에러가 발생했던 다른 사례들을 참고하시면 도움이 될 수 있습니다:

위 예시들을 통해 오류를 발생시키는 데이터의 유형을 파악하고, 어떻게 올바르게 수치형 데이터만 처리하여 문제를 해결하는지 확인해볼 수 있습니다.

저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다.
현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏
추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.

질문 드립니다.

0

30

2

강의 내용 관련 질문드립니다~

0

28

2

퍼플렉시티 최소 결제단위 50달러로 바뀐 것 같습니다.

0

29

2

수강 연장 문의

0

24

1

강의자료 일괄 다운로드

0

31

2

list 문제 질문드립니다~

0

25

2

빅분기 실기 12회 재도전

0

37

1

강의 기간 연장 가능여부 검토 요청건

0

27

1

수강기간 연장 문의 드립니다

0

33

2

claude plugin 방법 자세히 부탁드려요

0

33

2

kaggle data 분석 강의에 사용된 data file이 없읍니다.

0

70

1

데이터 수정

0

124

1

제가 뭘 틀린걸까요??ㅠ

0

202

1

섹션1의 마지막 강의(DataFrame에서 자주 사용하는 전처리 기법)의 오류 해결방법

0

174

1

파일을 읽어들일 때 질문 있습니다

0

231

1

선형 회귀 이론 및 실습 부터 전혀 이해가 안되네요.

0

288

1

섹션 4-2 13:57 보라색, 연두색 선?

0

234

1

seaborn에서 연습 데이터셋을 불러오는데 오류가 발생합니다.

0

481

1

데이터 자료

0

466

1

os.listdir(base_src) 오류

1

1437

1

[12:15] 훈련세트, 검증세트에 대한 예제 중 실행문이 안보입니다

1

237

1

에러 문의

1

415

1

CSV 파일 문의

1

704

1

수업에서 사용되는 csv파일은 어디서 구해야 합니까?

1

912

1