算法学习
、4对1辅导
、论文辅导
、核心期刊
项目的代码和数据下载
可以通过公众号
滴滴我
文章目录
一、项目背景
近年来,我国结婚、离婚呈现以下几个特点:
- 结婚数据方面
- 结婚登记数持续下降后有短暂回升但又出现下降趋势
- 结婚年龄不断推迟
- 适婚人口总数减少
- 地区差异明显
- 离婚数据方面
- 离婚登记数在达到顶峰后下降
- 离婚率有所回落
- 离婚冷静期制度影响明显
总体而言,我国结婚登记数呈下降趋势,离婚登记数在经历了一段时间的增长后也开始下降,这些变化受到适婚人口总数下降、婚姻观念变化、结婚成本上升、离婚冷静期制度等多种因素的综合影响。
二、数据说明
该数据共620行
,共9个
字段。分别是:地区
、年份
、结婚登记(万对)
、内地居民登记结婚(万对)
、内地居民初婚登记(万对)
、内地居民再婚登记(万对)
、涉外及港澳台居民登记结婚(万对)
、离婚登记(万对)
、粗离婚率(‰)
。
以下是表的部分数据:
三、数据分析
1、导入包以及读取数据
导入包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import font_manager
import seaborn as sns
关闭warn以及设置matplotlib设置中文
import warnings
warnings.filterwarnings('ignore')
from pylab import mpl
mpl.rcParams["font.sans-serif"] = ["SimHei"] # 设置显示中文字体 宋体
mpl.rcParams["axes.unicode_minus"] = False #字体更改后,会导致坐标轴中的部分字符无法正常显示,此时需要设置正常显示负号
2、数据读取及预览
df = pd.read_csv("data.csv")
df.head()
3、数据预处理
items = [
[
col,
df[col].dtype,
df[col].isnull().sum(),
] for col in df
]
display(pd.DataFrame(data=items, columns=[
'Attributes',
'Data Type',
'Total Missing',
]))
# 缺失值处理
df["tmp"] = df["结婚登记(万对)"] - df["内地居民登记结婚(万对)"]
df["涉外及港澳台居民登记结婚(万对)"].fillna(df["tmp"], inplace=True)
df.drop("tmp", axis=1, inplace=True)
# 重复着处理
df.drop_duplicates(inplace = True)
def clean_cityname(city_name):
if city_name.endswith("市") or city_name.endswith("省"):
res = city_name[:-1]
elif city_name != "内蒙古自治区":
res = city_name[:2]
else:
res = city_name[:3]
return res
df["地区"] = df["地区"].map(lambda x:clean_cityname(x))
4、数据可视化
a、近十年来(2013-2022)我国结婚离婚登记情况
# 近10年结婚/离婚人数
data = df.query("年份 > '2013'")
tmp = data.groupby("年份").agg({"结婚登记(万对)":"sum","离婚登记(万对)":"sum"})
fig = plt.figure(figsize=(12,6),dpi=100)
ax = fig.add_subplot(111)
total_width, n = 0.7, 2
width = total_width / n
x = np.arange(len(tmp.index))
x = x - (total_width - width) / 2
bar1 = plt.bar(x - width/2, tmp["结婚登记(万对)"], width=width, label="结婚登记(万对)", fc="#5698c3")
bar2 = plt.bar(x + width/2, tmp["离婚登记(万对)"], width=width, label="离婚登记(万对)", fc="#68b88e")
def annotate(ax, x, y, text, fc="#ff7777", y0=0):
y = y - 0.5
ax.annotate(
" " + text + " ",
xy=(x, y),
xycoords="data",
xytext=(0, 28),
textcoords="offset points",
color="white",
size=12,
va="center",
ha="center",
weight="bold",
bbox=dict(boxstyle="round", fc=fc, ec="none"),
arrowprops=dict(
arrowstyle="fancy, tail_width=1.2",
fc=fc, ec="none", patchA=None
),
)
plt.plot([x, x], [y, y0], color="black",
linestyle="-", linewidth=1, alpha=0.25)
annotate(ax, 7.45, 1000, "2021年起,离婚“冷静期”开始执行", fc="#68b88e")
for spine in ax.spines.values():
spine.set_visible(False)
ax.spines['bottom'].set_visible(True)
for a,b in zip(x-width,tmp["结婚登记(万对)"]):
plt.text(a+.01,b+10,b,color="#5698c3")
for a,b in zip(x,tmp["离婚登记(万对)"]):
plt.text(a+.01,b+10,b,color="#68b88e")
plt.xticks(x,tmp.index,fontsize=13,alpha=0.7)
plt.yticks([])
plt.legend(ncol=2,loc="upper center",frameon=False,fontsize=12)
plt.title("近10年(2013-2022)我国结婚离婚登记情况",fontsize=20)
plt.show()
总结
- 近10年,全国结婚登记总数呈下降趋势,并在2019年开始跌至1000万对以下。
- 在2019年及之前,我国每年离婚登记数量呈上升趋势,但在2019年之后呈下降趋势,尤其是在2021年起执行离婚“冷静期”,离婚登记数得到进一步下降。
b、近10年(2013-2022)我国粗离婚率变化趋势
tmp = data.groupby("年份").agg({"粗离婚率(‰)":"mean"}).round(2)
fig = plt.figure(figsize=(12,6),dpi=100)
ax = fig.add_subplot(111)
#plt.step(tmp.index, tmp["粗离婚率(‰)"], where='post')
plt.plot(tmp.index, tmp["粗离婚率(‰)"],linewidth=3,label='粗离婚率(‰)',color='#2c9678',zorder=0)
def annotate(ax, x, y, text, fc="#ff7777", y0=0):
y = y - 0.1
ax.annotate(
" " + text + " ",
xy=(x, y),
xycoords="data",
xytext=(0, 28),
textcoords="offset points",
color="white",
size=12,
va="center",
ha="center",
weight="bold",
bbox=dict(boxstyle="round", fc=fc, ec="none"),
)
plt.scatter(tmp["粗离婚率(‰)"].idxmax(), tmp["粗离婚率(‰)"].max(),zorder=1,s=tmp["粗离婚率(‰)"].max()*30)
annotate(ax, tmp["粗离婚率(‰)"].idxmax(), tmp["粗离婚率(‰)"].max(), str(tmp["粗离婚率(‰)"].max()) + "\n近年最高点", fc="#68b88e")
plt.scatter(tmp["粗离婚率(‰)"].idxmin(), tmp["粗离婚率(‰)"].min(),zorder=1,s=tmp["粗离婚率(‰)"].min()*30)
annotate(ax, tmp["粗离婚率(‰)"].idxmin(), tmp["粗离婚率(‰)"].min(), str(tmp["粗离婚率(‰)"].min()) + "\n近年最低点", fc="#68b88e")
for spine in ax.spines.values():
spine.set_visible(False)
ax.spines['bottom'].set_visible(True)
plt.ylim(0,5)
plt.grid(axis='y',linestyle='--')
plt.xticks(tmp.index,fontsize=12,alpha=0.7)
plt.yticks(np.arange(0,5.5,1),fontsize=10,alpha=0.7)
plt.legend(loc="upper center",frameon=False,fontsize=12)
plt.title("近10年(2013-2022)我国粗离婚率变化趋势",fontsize=20)
# 显示图形
plt.show()
总结
- 近10年间,我国此离婚率整体处于2‰ ~ 4‰,并在2021年执行离婚“冷静期”起,粗离婚率进一步下降。
- 2019年是近年的粗离婚率最高值,达到3.37‰,而粗离婚率最低值出现在2022年,低至2.08‰.
c、近10年(2013-2022)我国内地居民结婚对数变化
tmp = data.groupby("年份").agg({"内地居民初婚登记(万对)":"sum","内地居民再婚登记(万对)":"sum"})
fig = plt.figure(figsize=(12,6),dpi=100)
ax = fig.add_subplot(111)
bar1 = plt.bar(tmp.index, tmp["内地居民再婚登记(万对)"], width=0.4, fc="#68b88e", label="再婚登记(万对)")
bar2 = plt.bar(tmp.index, tmp["内地居民初婚登记(万对)"], bottom=tmp["内地居民再婚登记(万对)"]+95, width=0.4,fc="#5698c3",label="初婚登记(万对)")
plt.bar_label(bar1, color="#68b88e")
plt.bar_label(bar2, color="#5698c3")
for spine in ax.spines.values():
spine.set_visible(False)
ax.spines['bottom'].set_visible(True)
plt.xticks(tmp.index,fontsize=12,alpha=0.7)
plt.ylim(0,3000)
plt.yticks([])
plt.legend(loc="upper center",ncol=2,frameon=False,fontsize=12)
plt.title("近10年(2013-2022)我国内地居民结婚对数变化",fontsize=20)
plt.show()
总结:
- 近10年间,我国的初婚登记数量逐年下降,而再婚登记数量波动不大,整体处于300万 ~ 500万对之间
d、2022年我国各地区结婚对数占比
df2022 = data.query("年份 == '2022年'")
df2022["初婚占比(%)"] = df2022["内地居民初婚登记(万对)"].map(lambda x:round(x/df2022["内地居民初婚登记(万对)"].sum(),4)*100)
df2022["再婚占比(%)"] = df2022["内地居民再婚登记(万对)"].map(lambda x:round(x/df2022["内地居民再婚登记(万对)"].sum(),4)*100)
fig = plt.figure(figsize=(12,10),dpi=100)
ax = fig.add_subplot(111)
bar1 = plt.barh(df2022["地区"], df2022["初婚占比(%)"]*-1, fc="#68b88e", label="初婚占比")
bar2 = plt.barh(df2022["地区"], df2022["再婚占比(%)"]*+1, fc="#5698c3", label="再婚占比")
for spine in ax.spines.values():
spine.set_visible(False)
plt.bar_label(bar1, df2022["初婚占比(%)"].map(lambda x:str(round(x,2))+"%"), padding=2.5, color="#68b88e")
plt.bar_label(bar2, df2022["再婚占比(%)"].map(lambda x:str(round(x,2))+"%"), padding=2.5, color="#5698c3")
plt.xticks([])
plt.yticks(df2022["地区"],fontsize=12,alpha=0.6)
plt.gca().axes.get_yaxis().set_tick_params(which='both', size=0)
plt.legend(loc=(.75,1), ncol=2, frameon=False,fontsize=10)
plt.title("2022年我国各地区结婚对数占比",fontsize=20)
plt.show()
总结:
- 2022年中,初婚登记占比最高的地区是广东,占比达到9.21%,其次是河南以及江苏;而占比最低的地区是西藏,初婚登记占比仅有0.44%
- 再婚占比中,四川地区的占比最高,达到8.08%,其次是山东及河南,占比最低的是西藏,占比仅有0.11%
e、2022年我国各地区结婚登记对数占比及粗离婚率详情
area_dict = {
"东北地区":["辽宁","吉林","黑龙江"],
"东部地区":["北京","天津","河北","上海","江苏","浙江","福建","山东","广东","海南"],
"中部地区":["山西","安徽","江西","河南","湖北","湖南"],
"西部地区":["内蒙古","广西","重庆","四川","贵州","云南","西藏","陕西","甘肃","青海","宁夏","新疆"]
}
def get_area(cityname):
for key,value in area_dict.items():
if cityname in value:
return key
df2022["地区划分"] = df2022["地区"].map(lambda x:get_area(x))
df2022["结婚登记占比(%)"] = df2022["结婚登记(万对)"].map(lambda x:round(x/df2022["结婚登记(万对)"].sum(),4)*100)
df2022["离婚登记占比(%)"] = df2022["离婚登记(万对)"].map(lambda x:round(x/df2022["离婚登记(万对)"].sum(),4)*100)
x_g, y_g = round(df2022["结婚登记占比(%)"].mean(),2), round(df2022["粗离婚率(‰)"].mean(),2)
area_colors = ["#de1c31","#346c9c","#41ae3c","#fecc11"]
sns.set_palette(area_colors)
x1 = df2022["结婚登记占比(%)"].tolist()
y1 = df2022["粗离婚率(‰)"].tolist()
cityname = df2022["地区"].tolist()
fig = plt.figure(figsize=(8,8),dpi=100)
ax = fig.add_subplot(111)
sns.scatterplot(x=x1, y=y1, s=70, hue=df2022["地区划分"], zorder=2)
sns.scatterplot(x=[x_g], y=[y_g], color="#000", alpha=.5, s=200, zorder=2)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.spines["bottom"].set_visible(False)
for i in range(len(x)):
if cityname[i] != "吉林" and cityname[i] != "湖南" and cityname[i] != "陕西":
plt.annotate(cityname[i], (x[i]-.20, y[i]+.04), alpha=.7)
else:
plt.annotate(cityname[i], (x[i]-.55, y[i]-.02), alpha=.7)
plt.annotate("全国平均", (x_g-.6, y_g+.05), alpha=.7, fontsize=12)
plt.xticks(fontsize=12, alpha=.7)
plt.xlabel("结婚登记占比(%)", fontsize=16, alpha=.7)
plt.yticks(fontsize=12, alpha=.7)
plt.ylabel("粗离婚率(‰)", rotation=0, labelpad=55, fontsize=16, alpha=.7)
plt.grid(zorder=1,alpha=.35)
plt.gca().axvline(x=0, color='w')
plt.legend(frameon=False,fontsize=12)
plt.title("2022年我国各地区结婚登记对数占比及粗离婚率详情",fontsize=18,pad=20)
plt.show()
总结:
- 2022年中,结婚登记占比较高的三个地区分别是广东、河南以及四川,三者中粗离婚率较高的是四川。
- 上海、四川以及海南的粗离婚率较低,且三者的结婚登记占比也是较低的。