list에 한계치? 같은 게 있나요?
조회수 651회
안녕하세요. 파이썬에 입문하고 데이터 분석을 처음 해보고 있습니다. 밑의 코드와 같은 목표를 두고 코드를 작성하였는데, 문제는 for문에서 10까지만 진행이되고 그후로 오류가 납니다. 그래서 10씩 180번 해보자고 해봤지만 그래도 진행되진 않았습니다. 키오류라고 떠서 무언가 없다는 걸 의미하는 거 같은데 그부분도 잘 모르겠습니다.
어째서 안되는 지 알려주신다면 정말 감사하겠습니다.
import pandas as pd
import numpy as np
import openpyxl
# 데이터 불러오기
df=pd.read_csv("D:\\파이썬\\1학기 프로젝트\\전처리.csv")#
# store_id별로 전체 평균 매출액을 오름차순으로 엑셀에 저장하기
# store_id 별 전체 매출액
store_id_amount = df['amount'].groupby(df['store_id'])
store_id_amount_sum = store_id_amount.apply(sum)
# store_id 별 거래 건수
trade_time = []# 스토어별 거래건수
avg_income = []#스토어별 평균매출
store_id=[]#스토어id 리스트
for i in range(1800):
trade_time.append(len(df[df['store_id'] == i])) # 스토어별 거래건수
avg_income.append(store_id_amount_sum[i] / trade_time[i]) #스토어별 평균매출
store_id.append(i)
print(store_id)
PS D:\파이썬\ 프로젝트> python '.\EDA.py'
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Traceback (most recent call last):
File "C:\Users\김장식\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1625, in pandas._libs.hashtable.Int64HashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1632, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 11
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\파이썬\1학기 프로젝트\EDA.py", line 61, in <module>
avg_income.append(store_id_amount_sum[i] / trade_time[i]) #스토어별 평균매출
File "C:\Users\김장식\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\series.py", line 853, in __getitem__
return self._get_value(key)
File "C:\Users\김장식\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\series.py", line 961, in _get_value
loc = self.index.get_loc(label)
File "C:\Users\김장식\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3082, in get_loc
raise KeyError(key) from err
KeyError: 11
댓글 입력