43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
import pandas as pd
|
||
import numpy as np
|
||
|
||
# 设置随机种子,以便结果可重复
|
||
np.random.seed(42)
|
||
|
||
# 定义生成数据的行数
|
||
num_rows = 100 # 生成 100 行数据
|
||
|
||
# 创建空列表来存储生成的ID
|
||
material_ids = []
|
||
device_ids = []
|
||
product_ids = []
|
||
|
||
# 生成材料、设备、产品的ID,确保同一行内的ID不重复
|
||
for _ in range(num_rows):
|
||
mat_id = np.random.randint(0, 100) # 材料ID范围 0-99
|
||
dev_id = np.random.randint(100, 201) # 设备ID范围 100-199
|
||
|
||
# 确保产品ID在当前行与材料ID和设备ID不重复
|
||
while True:
|
||
prod_id = np.random.randint(0, 201)
|
||
if prod_id != mat_id and prod_id != dev_id:
|
||
break
|
||
|
||
material_ids.append(mat_id)
|
||
device_ids.append(dev_id)
|
||
product_ids.append(prod_id)
|
||
|
||
# 创建数据框,将三个ID列结合起来
|
||
df_ids = pd.DataFrame({
|
||
'材料id': material_ids,
|
||
'设备id': device_ids,
|
||
'产品id': product_ids
|
||
})
|
||
|
||
# 指定文件路径并保存为CSV文件
|
||
file_path_ids = '测试数据 material_device_product_ids.csv'
|
||
df_ids.to_csv(file_path_ids, index=False) # index=False 表示不保存行索引
|
||
|
||
# 打印文件路径
|
||
print(f"CSV 文件已生成,路径为: {file_path_ids}")
|