import pandas as pd import numpy as np import matplotlib.pyplot as plt 데이터 로드 data =…
- User
import pandas as pd import numpy as np import matplotlib.pyplot as plt
데이터 로드
data = pd.read_csv('C:/Code/lotto_data.csv', encoding='utf-8')
Check the column names to see what's actually in the DataFrame
print("Column names in the DataFrame:") print(data.columns)
If the column name is different, replace 'winning_num' with the correct column name
For example, if the column is named 'Winning_Num' in the CSV, then use that instead:
data['winning_numbers'] = data['Winning_Num'].str.split(',')
Assuming 'winning_num' is the correct column name, proceed with the rest of your code
try: # 데이터 전처리 data = data.dropna() # 결측치 제거 data['winning_numbers'] = data['winning_num'].str.split(',') # 새로운 'winning_numbers' 열 생성 data['winning_numbers'] = data['winning_numbers'].apply(lambda x: sorted([int(y) for y in x])) # 당첨 번호 정렬
Ensure the correct column names are used
data.columns = [col.lower() for col in data.columns]
지역별 당첨번호 데이터 가져오기
regional_data = data.groupby('region')['winning_numbers'].apply(list)
지역별 당첨 번호 분석
region_stats = regional_data.apply(lambda x: pd.Series(np.concatenate(x))).value_counts().sort_index()
지역별 당첨 번호 분포 시각화
fig, ax = plt.subplots(figsize=(10, 6)) region_stats.unstack().plot(kind='line', ax=ax) ax.set_title('Regional Winning Number Distribution') ax.set_xlabel('Number') ax.set_ylabel('Count') ax.set_xlim(1, 49) # Assuming lotto numbers range from 1 to 49 plt.show()
각 지역의 당첨자 수 계산
winners_by_region = data.groupby('region')['winners'].sum()
그래프 그리기
winners_by_region.plot(kind='bar') plt.title('Winners by Region') plt.xlabel('Region') plt.ylabel('Number of Winners') plt.show()
판매점별 당첨자 수 계산
winners_by_store = data.groupby('store')['winners'].sum()
그래프 그리기
winners_by_store.plot(kind='bar') plt.title('Winners by Store') plt.xlabel('Store') plt.ylabel('Number of Winners') plt.xticks(rotation=90) # Rotate x-axis labels for better readability plt.tight_layout() # Adjust layout to fit all labels plt.show()
전체 당첨 번호 분석
overall_stats = pd.Series(np.concatenate(data['winning_numbers'].values)).value_counts().sort_index()
전체 당첨 번호 분포 시각화
fig, ax = plt.subplots(figsize=(10, 6)) overall_stats.plot(kind='line', ax=ax) ax.set_title('Overall Winning Number Distribution') ax.set_xlabel('Number') ax.set_ylabel('Count') ax.set_xlim(1, 49) # Assuming lotto numbers range from 1 to 49 plt.show()
Assistant
- User
Column names in the DataFrame: Index(['Draw', 'Date', 'Num1', 'Num2', 'Num3', 'Num4', 'Num5', 'Num6', 'Bonus'], dtype='object') Traceback (most recent call last): File "C:\Code\myenv\lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc return self._engine.get_loc(casted_key) File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\hashtable_class_helper.pxi", line 7081, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 7089, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'winning_num'
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "c:\Code\빈파일_app1.py", line 14, in lotto_data['winning_numbers'] = lotto_data['winning_num'].str.split(',') # Split winning numbers File "C:\Code\myenv\lib\site-packages\pandas\core\frame.py", line 4102, in getitem indexer = self.columns.get_loc(key) File "C:\Code\myenv\lib\site-packages\pandas\core\indexes\base.py", line 3812, in get_loc raise KeyError(key) from err KeyError: 'winning_num'
Assistant
The error message is telling you that there is no column named 'winning_num' in your DataFrame. The column names that you have in your DataFrame are listed in the output of
print(lotto_data.columns).It seems that you are trying to split a column named 'winning_num' into a new column named 'winning_numbers', but this column does not exist.
You should replace
lotto_data['winning_num']with one of the existing column names. If you want to split the winning numbers into a new column, you should specify the correct column name.For example, if your winning numbers are stored in the 'Num1', 'Num2', ..., 'Num6' columns, you could split them like this:
codelotto_data['winning_numbers'] = lotto_data[['Num1', 'Num2', 'Num3', 'Num4', 'Num5', 'Num6']].apply(lambda x: list(x), axis=1)This will create a new column 'winning_numbers' with the winning numbers.
If your winning numbers are stored in a different column, you should replace
['Num1', 'Num2', 'Num3', 'Num4', 'Num5', 'Num6']with the correct column names.