【发布时间】:2020-02-04 04:25:56
【问题描述】:
我正在尝试生成一个数据集,其中给定年份范围内的每一天都有固定数量的商店。反过来,每家商店都销售固定数量的产品。每个商店和每一天的特定产品都有销售价值 (£) 和售出的产品数量。
但是,运行这些 for 循环需要一段时间来创建数据集。
有没有什么办法可以提高我的代码效率?
# Generate one row Dataframes (for concatenation) for each product, in each store, on each date
dataframes = []
for d in datelist:
for s in store_IDs:
for p in product_IDs:
products_sold = random.randint(1,101)
sales = random.randint(100,1001)
data_dict = {'Date': [d], 'Store ID': [s], 'Product ID': [p], 'Sales': [sales], 'Number of Products Sold': [products_sold]}
dataframe = pd.DataFrame(data_dict)
dataframes.append(dataframe)
test_dataframe = pd.concat(dataframes)
【问题讨论】:
-
你看下面我的回答了吗?
标签: python python-3.x performance for-loop