
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
sparta_data.head() 까지는 정상적으로 실행하는데
아래 코드 복붙 후 부터는 막히네요
제가 보기엔 영상 속 강사님과 코드가 같아보이는데 왜 실행이 되지 않는건가요?
작성한 코드 및 에러 메세지
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rc('font', family='NanumBarunGothic') #한글 깨짐 방지 글꼴 설정
sparta_data=pd.read_table('/content/user_db1.csv' ,sep=".")
sparta_data.head()
#sparta_data['group'] ==1 이라는 조건을 두고 그것이 일치하는 user의 수를 세면 됩니다!
#이때의 ==은 우리가 알고 있는 "equal" = 과 동일한 뜻입니다! (파이선에서 = 는 변수 지정 할때만 쓰입니다. 소곤소곤)
#할인 혜택 받지 않은 사람의 결제 수
sum_of_students_by_discounted = sparta_data[sparta_data['group']==1]['user_id'].count()
sum_of_students_by_discounted
오류 ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
KeyError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3801 try:
-> 3802 return self._engine.get_loc(casted_key)
3803 except KeyError as err:
4 frames
/usr/local/lib/python3.10/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
/usr/local/lib/python3.10/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'group'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
<ipython-input-50-173d1246214f> in <cell line: 13>()
11 #이때의 ==은 우리가 알고 있는 "equal" = 과 동일한 뜻입니다! (파이선에서 = 는 변수 지정 할때만 쓰입니다. 소곤소곤)
12 #할인 혜택 받지 않은 사람의 결제 수
---> 13 sum_of_students_by_discounted = sparta_data[sparta_data['group']==1]['user_id'].count()
14 sum_of_students_by_discounted
/usr/local/lib/python3.10/dist-packages/pandas/core/frame.py in __getitem__(self, key)
3805 if self.columns.nlevels > 1:
3806 return self._getitem_multilevel(key)
-> 3807 indexer = self.columns.get_loc(key)
3808 if is_integer(indexer):
3809 indexer = [indexer]
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3802 return self._engine.get_loc(casted_key)
3803 except KeyError as err:
-> 3804 raise KeyError(key) from err
3805 except TypeError:
3806 # If we have a listlike key, _check_indexing_error will raise
KeyError: 'group'
