Last active
April 30, 2025 04:16
-
-
Save aipro-jp/e07a85b4c4aa29547ee46c8f2851a2c4 to your computer and use it in GitHub Desktop.
集計用サンプルExcelファイル作成
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import os | |
import numpy as np # 乱数生成のためにnumpyをインポート | |
# サンプルファイルを作成するフォルダ名 | |
# このスクリプトと同じ場所にファイルが保存されます。 | |
output_dir = '.' # 現在のディレクトリを指定 | |
# 作成する支店リストとファイル名パターン | |
branches = ['東京支店', '大阪支店', '福岡支店', '名古屋支店', '札幌支店'] | |
file_name_suffix = '_売上.xlsx' | |
# サンプルデータ生成関数 | |
def create_sample_data(branch_name, num_rows=10): | |
""" | |
指定された支店のサンプル売上データをDataFrameとして生成する関数。 | |
支店名によって売上個数にばらつきを持たせ、単価は商品ごとに固定とします。 | |
""" | |
# 支店名に応じて乱数のシードを設定し、毎回同じ支店名なら同じデータになるようにする | |
# ただし、異なる支店名ならデータは異なる | |
np.random.seed(hash(branch_name) % (2**32 - 1)) # 支店名からシードを生成 | |
start_date = '2025-03-01' | |
dates = pd.to_datetime(pd.date_range(start=start_date, periods=num_rows, freq='D')) | |
# 商品リストと固定単価を定義 | |
products = ['商品A', '商品B', '商品C', '商品D', '商品E'] | |
# 各商品の単価(円) | |
unit_prices = { | |
'商品A': 1000, | |
'商品B': 1500, | |
'商品C': 800, | |
'商品D': 2500, | |
'商品E': 1200 | |
} | |
# 各日付でランダムに商品を選択 | |
product_names = np.random.choice(products, size=num_rows, replace=True) | |
# 売上個数に支店ごとのばらつきとランダム性を加える | |
# 支店ごとに異なるベース個数を設定(例として支店名のハッシュ値を利用) | |
branch_base_counts = (hash(branch_name) % 5 + 1) * 5 # 支店ごとに異なるベース個数 | |
# 売上個数:ベース個数 + 日付による増加傾向 + ランダムな変動 + 支店による補正 | |
sales_counts = [int(branch_base_counts + i * 0.3 + np.random.randint(-2, 3) + (hash(branch_name) % 5 - 2)) for i in range(num_rows)] | |
sales_counts = [max(1, count) for count in sales_counts] # 最小値は1とする | |
# 売上金額:売上個数 × 商品の単価 で計算し、100単位に丸める | |
sales_amounts = [int((sales_counts[i] * unit_prices[product_names[i]]) / 100) * 100 for i in range(num_rows)] | |
sales_amounts = [max(unit_prices[product_names[i]], amount) for i, amount in enumerate(sales_amounts)] # 単価を下回らないようにする | |
data = { | |
'日付': dates, | |
'商品名': product_names, | |
'売上個数': sales_counts, | |
'売上金額': sales_amounts | |
} | |
df = pd.DataFrame(data) | |
return df | |
# 各支店のサンプルファイルを作成 | |
print("\nサンプルExcelファイルの作成を開始します...") | |
for branch in branches: | |
file_name = f"{branch}{file_name_suffix}" | |
# output_dir が '.' なので、os.path.join は不要ですが、明示的に指定することも可能です | |
# output_path = os.path.join(output_dir, file_name) | |
output_path = file_name # スクリプトと同じディレクトリに作成 | |
# サンプルデータを生成(支店ごとに異なるデータを生成) | |
sample_df = create_sample_data(branch) | |
# Excelファイルとして保存 | |
try: | |
# ExcelWriterを使用してエンジンを指定(openpyxlを使用) | |
with pd.ExcelWriter(output_path, engine='openpyxl') as writer: | |
sample_df.to_excel(writer, index=False, sheet_name='Sheet1') # sheet_nameを指定 | |
print(f"'{output_path}' を作成しました。") | |
except Exception as e: | |
print(f"エラー: '{output_path}' の作成中に問題が発生しました。詳細: {e}") | |
print("\nサンプルExcelファイルの作成が完了しました。") | |
print(f"スクリプトと同じディレクトリにファイルが作成されています。") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment