import pandas as pd
import random
# 定义生成数据的函数
def generate_random_survey_data(num_entries=800):
data = []
for _ in range(num_entries):
entry = {
"调查员编号": random.randint(1000, 9999),
"地点": f"地点_{random.randint(1, 100)}",
"时间": f"{random.randint(1, 12)}月{random.randint(1, 28)}日",
"人口类型": random.choice(["外来人口", "本地人口"]),
"性别": random.choice(["男", "女"]),
"年龄": random.randint(18, 80),
"就业状况": random.choice(["失业", "就业", "退休"]),
"教育程度": random.choice(["小学及以下", "初中和高中", "大专及以上"]),
"婚姻情况": random.choice(["单身", "已婚", "离异", "丧偶"]),
"家庭规模": random.randint(1, 6),
"居住面积": random.randint(20, 150),
"房间个数": random.randint(1, 5),
"阳台面积": random.randint(0, 20),
"共同生活的几代人": random.choice(["一", "二", "三及以上"]),
"主要收入来源": random.choice(["工资", "商业", "退休金"]),
"月收入": random.randint(1000, 20000),
"居住时间": random.randint(1, 30),
"多久回一次老家": random.choice(["每月", "每周", "每日", "每年"]),
"通勤模式": random.choice(["步行", "自行车", "公共交通", "私家车"]),
"通勤时间": random.choice(["15分钟或更短", "15-30分钟", "30-60分钟", "1-2小时", "2小时及以上"]),
}
# 添加Likert量表的问题回答
for i in range(15, 50):
entry[f"问题_{i}"] = random.randint(1, 6)
data.append(entry)
return data
# 生成800份问卷数据
data = generate_random_survey_data(800)
# 转换为DataFrame
df = pd.DataFrame(data)
# 保存为Excel文件
df.to_excel("survey_data.xlsx", index=False)
print("数据已成功生成并保存为survey_data.xlsx")