inflearn logo
강의

Khóa học

Chia sẻ kiến thức

Học máy được xây dựng từ các thư viện Python cơ bản

Các kỹ thuật tiền xử lý thường được sử dụng trong DataFrame

11분 55초 오류

116

edudonghyeon9899

1 câu hỏi đã được viết

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

Câu trả lời 1

0

communityai8509

안녕하세요, 인프런 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()
    

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

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

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

강사님 안녕하세요 ㅎdatabricks환경에 대한 설

0

4

1

6-6

0

15

1

작업형 1 유형 부분

0

13

1

import torch가 안되는 경우는 어떻게 하나요?

0

17

1

작업형 1 (삭제예정, 구 버전)

0

34

2

강의노트는 어디있나요?

0

21

1

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

0

23

1

2유형 레이블 인코딩 VS 원핫 인코딩

0

25

3

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

0

27

1

인덱스 슬라이싱

0

28

2

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

0

71

1

데이터 수정

0

124

1

제가 뭘 틀린걸까요??ㅠ

0

204

1

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

0

176

1

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

0

231

1

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

0

289

1

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

0

236

1

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

0

482

1

데이터 자료

0

467

1

os.listdir(base_src) 오류

1

1437

1

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

1

237

1

에러 문의

1

415

1

CSV 파일 문의

1

704

1

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

1

912

1