• 카테고리

    질문 & 답변
  • 세부 분야

    데이터 분석

  • 해결 여부

    미해결

질문있어요!

21.02.18 23:16 작성 조회수 164

0

df.loc[df['age']>=30, ['name', 'value']]

이 부분이 

TypeError                                 Traceback (most recent call last)
<ipython-input-108-dd0bf85b7488> in <module>
----> 1 df.loc[df['age']>=30, ['name', 'value']]

~\anaconda3\lib\site-packages\pandas\core\ops\common.py in new_method(self, other)
     63         other = item_from_zerodim(other)
     64 
---> 65         return method(self, other)
     66 
     67     return new_method

~\anaconda3\lib\site-packages\pandas\core\ops\__init__.py in wrapper(self, other)
    368         rvalues = extract_array(other, extract_numpy=True)
    369 
--> 370         res_values = comparison_op(lvalues, rvalues, op)
    371 
    372         return self._construct_result(res_values, name=res_name)

~\anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in comparison_op(left, right, op)
    242 
    243     elif is_object_dtype(lvalues.dtype):
--> 244         res_values = comp_method_OBJECT_ARRAY(op, lvalues, rvalues)
    245 
    246     else:

~\anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in comp_method_OBJECT_ARRAY(op, x, y)
     54         result = libops.vec_compare(x.ravel(), y.ravel(), op)
     55     else:
---> 56         result = libops.scalar_compare(x.ravel(), y, op)
     57     return result.reshape(x.shape)
     58 

pandas\_libs\ops.pyx in pandas._libs.ops.scalar_compare()
TypeError: '>=' not supported between instances of 'str' and 'int'
이렇게 오류가 뜨는데 어떻게 해결해야 하나요??


답변 2

·

답변을 작성해보세요.

0

JAE-HOON JEONG님의 프로필

JAE-HOON JEONG

2021.09.03

해결하셨을 거 같긴한데, 혹시 도움이 될까 싶어 댓글 남겨요.

저도 웹크롤링한 다음 df.info로 보면 age, number가 int가 아니라 object로 나오더라구요. 그래서 전 웹크롤링해서 받는 코드를 int()로 감싸 int64로 형태를 변환시켰습니다. 이렇게 하면 int64로 age 내 value 형태가 변해서 위 에러가 없어질겁니다. 아래에 뷰티풀숲으로 코딩한 내용 중 int로 감싼 내용만 복붙할게요~

 for i in player_info:

a = i.find_all('td')
ranking = int(a[0].text)
name = a[3].text
position = a[4].text
age = int(a[5].text)
nation = a[6].img['alt']
team = a[7].img['alt']
value = a[8].text.strip()

player_list.append([ranking, name, position, age, nation, team, value])

time.sleep(2)

0

안녕하세요?

문자와 정수형 숫자를 비교할 때 생기는 오류 메시지인데..df['age'] >=30 이 부분에서 문제가 생긴 것 같습니다. df.info()로 'age'칼럼이 정수형인지(int64)  또 빠진 값은 없는 지 다시 확인해보실래요?