import pandas as pd from scipy import stats # 讀取數據 data = { "廣告": ["廣告1"] * 100 + ["廣告2"] * 100, "消費金額": [ 606.9, 543, 370.2, 722.4, 671.7, 593.7, 581.7, 562.8, 648, 706.2, 721.5, 399.3, 374.7, 365.4, 554.4, 543.9, 767.1, 754.5, 620.4, 596.1, 243.6, 361.2, 723.6, 925.2, 583.2, 431.1, 552.3, 484.8, 574.8, 359.7, 288, 288.6, 293.4, 347.7, 461.4, 566.7, 704.4, 262.5, 470.4, 332.4, 643.5, 65.4, 584.4, 702, 697.5, 595.5, 435.9, 339.9, 653.1, 920.7, 354.9, 512.4, 656.1, 454.8, 622.8, 400.8, 258, 456.6, 439.2, 616.2, 257.1, 555, 461.4, 352.8, 421.8, 406.2, 462, 950.4, 388.8, 546.3, 612.3, 424.2, 570.9, 220.8, 474.6, 480.3, 495, 527.4, 349.5, 507.6, 360.6, 441.6, 774.3, 292.5, 534.6, 606.6, 685.8, 354.3, 401.1, 326.7, 335.7, 708, 461.1, 556.8, 143.4, 487.2, 213, 575.7, 457.8, 189.3, 36.6, 297.3, 452.7, 521.7, 439.2, 147.9, 390.6, 200.1, 327.9, 370.8, 631.8, 221.1, 99.9, 213.6, 175.8, 590.7, 328.2, 257.7, 510, 180, 624.9, 643.5, 812.7, 422.7, 936.9, 816, 799.5, 666.6, 650.4, 678.6, 386.7, 515.1, 475.8, 432.3, 446.4, 318.6, 504.3, 345.6, 475.2, 501, 549, 737.1, 652.2, 616.8, 520.5, 511.2, 606, 591.9, 726.9, 424.2, 555.6, 559.2, 468.3, 648.6, 396.6, 424.8, 439.5, 591.3, 535.2, 585.9, 455.4, 613.2, 799.8, 609, 888.9, 716.1, 430.8, 629.7, 455.7, 641.7, 642.9, 534.3, 280.8, 883.5, 380.7, 429.6, 293.7, 662.4, 438.9, 350.4, 239.4, 241.5, 245.7, 544.2, 850.2, 182.4, 393.9, 351.3, 530.7, 531.6, 223.2, 542.1, 798.6, 207, 343.2, 808.8, 537, 325.2, 476.4, 305.1, 449.7, 480, 550.5, 196.2, 207.6, 438, 739.8, 522, 531.6, 357, 504.3, 696.3, 568.8, 581.4, 107.4, 580.5, 378.9, 141.6, 372.9, 869.1, 375.6, 760.8, 252.3, 757.8, 533.1, 546.9, 269.1, 356.7, 420.9, 465, 528.6, 462.9, 504.6, 621, 460.2, 595.2, 412.5, 570, 666.9, 444.3 ] } df = pd.DataFrame(data) # 分組 group1 = df[df["廣告"] == "廣告1"]["消費金額"] group2 = df[df["廣告"] == "廣告2"]["消費金額"] # 獨立樣本 t 檢定 t_stat, p_value = stats.ttest_ind(group2, group1, equal_var=False) # 顯示結果 t_stat, p_value