import pandas as pd
from scipy.stats import norm
import numpy as np
# 生成 Z 值从 0到 3.4 的数据,步长为 0.01
z_values = np.arange(0, 3.5, 0.01)
cdf_values = norm.cdf(z_values) - norm.cdf(0) # 计算每个 Z 值的累积分布函数值
# 创建 DataFrame
df = pd.DataFrame({
'Z': z_values,
'CDF': cdf_values
})
# 将 Z 值分解为整数部分和小数部分,并分别处理
df['Z_int'] = df['Z'].apply(lambda x: int(x*10)/10)
df['Z_dec'] = df['Z'].apply(lambda x: round(x - int(x*10)/10, 2))
# 重塑 DataFrame,使其结构类似于表格格式
pivot_table = df.pivot_table(index='Z_int', columns='Z_dec', values='CDF', aggfunc='first')
# 格式化 CDF 值到五位小数
formatted_pivot = pivot_table.map(lambda x: f"{x:.5f}")
# # 保存 DataFrame 到 Excel 文件
# excel_path = 'Standard_Normal_Distribution_Table.xlsx'
# with pd.ExcelWriter(excel_path) as writer:
# formatted_pivot.to_excel(writer, sheet_name='Formatted Z Table')
# excel_path
formatted_pivot
