
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
2-4 3:57 전후에 있는 내용인데요. 'date'를 지웠는데, 그 다음에 다시 실행하면 아래와 같은 메세지가 나와요. df에 date가 없는데 지우라고 하니까 그러나보다 하고 생각이 들긴 하는데 강의할 때는 아무 문제 없이 그냥 된 거 같거든요. 그래서 이상해서 여쭤봅니다. 그래서 그 부분을 주석처리하고 다시 실행하면 'date'가 없이 출력이 되긴 합니다.
작성한 코드 및 에러 메세지
오류 발생 시cond = df['change_rate'] > 0
df[cond]
df = df[cond]#위에서 만든 파일을 df에 저장해.
cond = df['per'] > 0
df = df[cond]
#이제부터는 순이익과 종가를 추가해줄거야.
df['close'] = df['per']*df['eps']#eps=주당순이익, close = 종가 #지금은 close라는 열 만듦
df['earning']= df['marketcap']/df['per']#earning = 순이익=시가총액/per
del df['date'] #날짜가 다 똑같으니까 지워줘도 되겠군.
cond = (df['pbr'] < 1 ) & (df['marketcap'] > 1000000000000) & (df['per'] < 20 )
df = df[cond]
df
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
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: 'date'
The above exception was the direct cause of the following exception:
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)
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: 'date'
