Limit on the list? Is there something like that?

Asked 2 years ago, Updated 2 years ago, 88 views

Hello, this is my first time to analyze data since I entered Python. I wrote the code with the same goal as the code below, but the problem only progresses to 10 in the For Moon There's been an error after that. So I tried to do it 180 times, 10 times each, but it didn't work out. I think it means that there's nothing because it's called a kiosk, but I'm not sure about that either.

I'd really appreciate it if you could tell me why not.

import pandas as pd
import numpy as np
import openpyxl

# Loading data
df=pd.read_csv("D:\\Python\\First Semester Project\\Preprocessing.csv")#

# Store total average sales in Excel in ascending order by store_id

# Total sales by store_id
store_id_amount = df['amount'].groupby(df['store_id'])
store_id_amount_sum = store_id_amount.apply(sum)

# Number of transactions by store_id
trade_time = []# Number of transactions by store
avg_income = []#Average sales by store
store_id=[]#StoreidList

for i in range(1800):
    trade_time.append(len(df[df['store_id'] == i]))) # Number of transactions by store
    avg_income.append(store_id_amount_sum[i] / trade_time[i]) #average sales by store
    store_id.append(i)
    print(store_id)

PSD:\Python\Project> 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\Kim Decorations\AppData\Local\Programs\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3080, inget_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:\Python\First Semester Project\EDA.py", line 61, in <module>
    avg_income.append(store_id_amount_sum[i] / trade_time[i]) #average sales by store
  File "C:\Users\Kim Decorations\AppData\Local\Programs\Python39\lib\site-packages\pandas\core\series.py", line 853, in __getitem___
    return self._get_value(key)
  File "C:\Users\Kim Decorations\AppData\Local\Programs\Python39\lib\site-packages\pandas\core\series.py", line 961, in _get_value
    loc = self.index.get_loc(label)
  File "C:\Users\Kim Decorations\AppData\Local\Programs\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3082, inget_loc
    raise KeyError(key) from err
KeyError: 11

python for list

2022-09-20 16:26

1 Answers

KeyError occurred

avg_income.append(store_id_amount_sum[i] / trade_time[i])

where store_id_amount_sum[11] does not exist, or trade_time[11] does not exist. (I mean, it went up to 10, so there's up to 10.)

And... Errors may occur when using other Python packages because of the Korean user name. Please use the user name in English.


2022-09-20 16:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.