관리 메뉴

RUBY

[CCTV] 3.Pandas로 데이터 읽기(3) 본문

데이터 분석/EDA_웹크롤링_파이썬프로그래밍

[CCTV] 3.Pandas로 데이터 읽기(3)

ruby-jieun 2023. 2. 1. 00:38

 

 

 

서울시 CCTV 분석하기 프로젝트
3.Pandas로 데이터 읽기(3)


 

 

 

48.

CCTV_Seoul = pd.read_csv("../data/01.Seoul_CCTV.csv", encoding='cp949')
CCTV_Seoul.head()

CCTV의 앞 부분 데이터를 확인한다.

 

 

 

49. 가장 CCTV를 적게 보유한 구 확인하기

CCTV_Seoul.sort_values(by="총계", ascending=True).head(5)

 

 

 

50. 가장 CCTV를 많이 보유한 구 확인하기

CCTV_Seoul.sort_values(by="총계", ascending=False).head(5)

 

 

 

 

51. 최근증가율을 구하려는데 Type 오류가 뜬다. 해결해보자.

TypeError                                 Traceback (most recent call last)
File ~\miniconda3\envs\ds_study\lib\site-packages\pandas\core\ops\array_ops.py:165, in _na_arithmetic_op(left, right, op, is_cmp)
    164 try:
--> 165     result = func(left, right)
    166 except TypeError:

File ~\miniconda3\envs\ds_study\lib\site-packages\pandas\core\computation\expressions.py:241, in evaluate(op, a, b, use_numexpr)
    239     if use_numexpr:
    240         # error: "None" not callable
--> 241         return _evaluate(op, op_str, a, b)  # type: ignore[misc]
    242 return _evaluate_standard(op, op_str, a, b)

File ~\miniconda3\envs\ds_study\lib\site-packages\pandas\core\computation\expressions.py:129, in _evaluate_numexpr(op, op_str, a, b)
    128 if result is None:
--> 129     result = _evaluate_standard(op, op_str, a, b)
    131 return result

File ~\miniconda3\envs\ds_study\lib\site-packages\pandas\core\computation\expressions.py:70, in _evaluate_standard(op, op_str, a, b)
     69     _store_test_result(False)
---> 70 return op(a, b)

TypeError: unsupported operand type(s) for /: 'str' and 'str'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[50], line 2
      1 CCTV_Seoul["최근증가율"]=(
----> 2                     (CCTV_Seoul["2021년"] + CCTV_Seoul["2020년"]+ CCTV_Seoul["2019년"]+ CCTV_Seoul["2018년"]
      3                      + CCTV_Seoul["2017년"]+ CCTV_Seoul["2016년"]+ CCTV_Seoul["2015년"]+ CCTV_Seoul["2014년"]
      4                      + CCTV_Seoul["2013년"]+ CCTV_Seoul["2012년"]) 
      5                     / CCTV_Seoul["2012년 이전"]*100
      6                     )
      7 CCTV_Seoul.sort_values(by="최근증가율", ascending=False).head(5)

File ~\miniconda3\envs\ds_study\lib\site-packages\pandas\core\ops\common.py:72, in _unpack_zerodim_and_defer.<locals>.new_method(self, other)
     68             return NotImplemented
     70 other = item_from_zerodim(other)
---> 72 return method(self, other)

File ~\miniconda3\envs\ds_study\lib\site-packages\pandas\core\arraylike.py:126, in OpsMixin.__truediv__(self, other)
    124 @unpack_zerodim_and_defer("__truediv__")
    125 def __truediv__(self, other):
--> 126     return self._arith_method(other, operator.truediv)

File ~\miniconda3\envs\ds_study\lib\site-packages\pandas\core\series.py:6259, in Series._arith_method(self, other, op)
   6257 def _arith_method(self, other, op):
   6258     self, other = ops.align_method_SERIES(self, other)
-> 6259     return base.IndexOpsMixin._arith_method(self, other, op)

File ~\miniconda3\envs\ds_study\lib\site-packages\pandas\core\base.py:1325, in IndexOpsMixin._arith_method(self, other, op)
   1322 rvalues = ensure_wrapped_if_datetimelike(rvalues)
   1324 with np.errstate(all="ignore"):
-> 1325     result = ops.arithmetic_op(lvalues, rvalues, op)
   1327 return self._construct_result(result, name=res_name)

File ~\miniconda3\envs\ds_study\lib\site-packages\pandas\core\ops\array_ops.py:226, in arithmetic_op(left, right, op)
    222     _bool_arith_check(op, left, right)
    224     # error: Argument 1 to "_na_arithmetic_op" has incompatible type
    225     # "Union[ExtensionArray, ndarray[Any, Any]]"; expected "ndarray[Any, Any]"
--> 226     res_values = _na_arithmetic_op(left, right, op)  # type: ignore[arg-type]
    228 return res_values

File ~\miniconda3\envs\ds_study\lib\site-packages\pandas\core\ops\array_ops.py:172, in _na_arithmetic_op(left, right, op, is_cmp)
    166 except TypeError:
    167     if not is_cmp and (is_object_dtype(left.dtype) or is_object_dtype(right)):
    168         # For object dtype, fallback to a masked operation (only operating
    169         #  on the non-missing values)
    170         # Don't do this for comparisons, as that will handle complex numbers
    171         #  incorrectly, see GH#32047
--> 172         result = _masked_arith_op(left, right, op)
    173     else:
    174         raise

File ~\miniconda3\envs\ds_study\lib\site-packages\pandas\core\ops\array_ops.py:110, in _masked_arith_op(x, y, op)
    108     # See GH#5284, GH#5035, GH#19448 for historical reference
    109     if mask.any():
--> 110         result[mask] = op(xrav[mask], yrav[mask])
    112 else:
    113     if not is_scalar(y):

TypeError: unsupported operand type(s) for /: 'str' and 'str'

 

 

   1)엑셀로 csv파일을 열고 데이터를 선택한 뒤 셀 서식에 들어간다.

  2) 셀서식에서 숫자를 선택해준다.

 3) 파일 저장 후 jupyter에서 다시 불러와 준 뒤 입력하면 정상적으로 출력된다. 

CCTV_Seoul = pd.read_csv("../data/01.Seoul_CCTV.csv", encoding='cp949')
CCTV_Seoul["최근증가율"]=((CCTV_Seoul["2021년"] + CCTV_Seoul["2020년"]+ CCTV_Seoul["2019년"]+ CCTV_Seoul["2018년"]
                     + CCTV_Seoul["2017년"]+ CCTV_Seoul["2016년"]+ CCTV_Seoul["2015년"]+ CCTV_Seoul["2014년"]
                     + CCTV_Seoul["2013년"]+ CCTV_Seoul["2012년"])/ CCTV_Seoul["2012년 이전"]*100)
CCTV_Seoul.sort_values(by="최근증가율", ascending=False).head(5)
Comments