2025妈妈杯B题原创python代码+结果表|全部可运行|精美可视化

以下是部分数据预处理和第一问的代码:

import pandas as pd

df = pd.read_excel("data/附件一:老城街区地块信息.xlsx")

df = df.rename(columns={
    "地块ID": "plot_id",
    "院落ID": "courtyard_id",
    "地块面积": "plot_area",
    "院落面积": "courtyard_area",
    "地块方位": "orientation",
    "是否有住户": "is_occupied"
})

df["is_occupied"] = df["is_occupied"].astype(bool)
orientation_score = {"南": 3, "北": 3, "东": 2, "西": 1}
df["orientation_score"] = df["orientation"].map(orientation_score)

df.to_csv("标准化地块信息数据表.csv", index=False)




import pandas as pd

df = pd.read_csv("标准化地块信息数据表.csv")
residents = df[df["is_occupied"] == True].copy()
vacants = df[df["is_occupied"] == False].copy()

candidates = []
for _, res in residents.iterrows():
    for _, vac in vacants.iterrows():
        area_ok = vac["plot_area"] >= res["plot_area"] and vac["plot_area"] <= 1.3 * res["plot_area"]
        light_ok = vac["orientation_score"] >= res["orientation_score"]
        needs_repair = not (area_ok and light_ok)
        if area_ok or light_ok:
            candidates.append({
                "原地块ID": res["plot_id"],
                "原院落ID": res["courtyard_id"],
                "原面积": res["plot_area"],
                "原采光评分": res["orientation_score"],
                "候选地块ID": vac["plot_id"],
                "候选院落ID": vac["courtyard_id"],
                "候选面积": vac["plot_area"],
                "候选采光评分": vac["orientation_score"],
                "是否需修缮补偿": needs_repair
            })

candidates_df = pd.DataFrame(candidates)
candidates_df.to_csv("搬迁可行对候选表.csv", index=False)




import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.sans-serif'] = ['SimHei']        # 使用黑体
matplotlib.rcParams['axes.unicode_minus'] = False          # 正常显示负号

# 读取 Excel 文件(根据你的目录结构)
df = pd.read_excel("data/附件一:老城街区地块信息.xlsx")

# 字段清洗与标准化
df = df.rename(columns={
    "地块ID": "plot_id",
    "院落ID": "courtyard_id",
    "地块面积": "plot_area",
    "院落面积": "courtyard_area",
    "地块方位": "orientation",
    "是否有住户": "is_occupied"
})

df["is_occupied"] = df["is_occupied"].astype(bool)
orientation_score = {"南": 3, "北": 3, "东": 2, "西": 1}
df["orientation_score"] = df["orientation"].map(orientation_score)

# 分离居住地块和空置地块
residents = df[df["is_occupied"] == True].copy()
vacants = df[df["is_occupied"] == False].copy()

# 构建搬迁候选对
candidates = []
for _, res in residents.iterrows():
    for _, vac in vacants.iterrows():
        area_ok = vac["plot_area"] >= res["plot_area"] and vac["plot_area"] <= 1.3 * res["plot_area"]
        light_ok = vac["orientation_score"] >= res["orientation_score"]
        needs_repair = not (area_ok and light_ok)
        if area_ok or light_ok:
            candidates.append({
                "原地块ID": res["plot_id"],
                "原院落ID": res["courtyard_id"],
                "原面积": res["plot_area"],
                "原采光": res["orientation"],
                "原采光评分": res["orientation_score"],
                "候选地块ID": vac["plot_id"],
                "候选院落ID": vac["courtyard_id"],
                "候选面积": vac["plot_area"],
                "候选采光": vac["orientation"],
                "候选采光评分": vac["orientation_score"],
                "是否需修缮补偿": needs_repair
            })

candidates_df = pd.DataFrame(candidates)

# 统计每个居民原地块的可搬迁目标数量
candidate_counts = candidates_df["原地块ID"].value_counts().sort_index()

# 绘图
plt.figure(figsize=(10, 5))
plt.bar(candidate_counts.index, candidate_counts.values)
plt.xlabel("原地块ID")
plt.ylabel("可搬迁目标数量")
plt.title("每个居民地块的可搬迁候选数量分布")
plt.tight_layout()
plt.savefig("figure1.pdf")  # 生成 PDF 文件
plt.show()

可视化结果:

以上仅为部分。其中更详细的思路、各题目思路、代码、讲解视频、成品论文及其他相关内容,可以点击下方名片哦!

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐