It's a very good detailed answer provided by @BruceET. So if you want to do it python, you have to calculate the pooled standard error. I moved the code from this link and you can see it gives you something similar to Bruce's answer:

import numpy as np
from scipy.stats import ttest_ind
from scipy.stats import t
import pandas as pd

def welch_ttest(x1, x2,alternative):
    
    n1 = x1.size
    n2 = x2.size
    m1 = np.mean(x1)
    m2 = np.mean(x2)
    
    v1 = np.var(x1, ddof=1)
    v2 = np.var(x2, ddof=1)
    
    pooled_se = np.sqrt(v1 / n1 + v2 / n2)
    delta = m1-m2
    
    tstat = delta /  pooled_se
    df = (v1 / n1 + v2 / n2)**2 / (v1**2 / (n1**2 * (n1 - 1)) + v2**2 / (n2**2 * (n2 - 1)))
    
    # two side t-test
    p = 2 * t.cdf(-abs(tstat), df)
    
    # upper and lower bounds
    lb = delta - t.ppf(0.975,df)*pooled_se 
    ub = delta + t.ppf(0.975,df)*pooled_se
  
    return pd.DataFrame(np.array([tstat,df,p,delta,lb,ub]).reshape(1,-1),
                         columns=['T statistic','df','pvalue 2 sided','Difference in mean','lb','ub'])

We run this function, i named the lower and upper bounds of the 95% CI as lb and ub.. You can simply modify them in the function:

welch_ttest(ts1,ts2,"equal")


    T statistic     df  pvalue 2 sided  Difference in mean  lb  ub
0   -1.832542   17.90031    0.08356     -1.0    -2.146912   0.146912
Answer from StupidWolf on Stack Exchange
🌐
Socscistatistics
socscistatistics.com › confidenceinterval › default4.aspx
Confidence Interval Calculator: Independent Samples T-Test
This simple confidence interval calculator uses a t statistic and two sample means (M1 and M2) to generate an interval estimate of the difference between two population means (μ1 and μ2).
🌐
Stats
stats.blue › Stats_Suite › two_sample_t_confidence_interval.html
Two-Sample t - t - Confidence Interval Calculator
Calculate a Two-Sample t Confidence Interval Comparing the Difference of Two Means with our Free, Easy-To-Use, Online Statistical Software.
🌐
PubMed Central
pmc.ncbi.nlm.nih.gov › articles › PMC3699740
Confidence intervals for two sample means: Calculation, interpretation, and a few simple rules - PMC
Focusing on a simple but prevalent ... of two sample means, we describe possible CIs for between- and within-subjects designs. In addition, we give hands-on examples of how to compute these CIs and discuss their relation to classical t-tests. Keywords: confidence intervals, graphical ...
🌐
Evanmiller
evanmiller.org › ab-testing › t-test.html
Two-Sample T-Test (Evan’s Awesome A/B Tools)
Sample Size Calculator | Chi-Squared Test | Sequential Sampling | 2 Sample T-Test | Survival Times | Count Data
🌐
GraphPad
graphpad.com › quickcalcs › ttest1
T test calculator
Performs unpaired t test, Weldh's t test (doesn't assume equal variances) and paired t test. Calculates exact P value and 95% confidence interval. Clear results with links to extensive explanations.
Top answer
1 of 4
10

It's a very good detailed answer provided by @BruceET. So if you want to do it python, you have to calculate the pooled standard error. I moved the code from this link and you can see it gives you something similar to Bruce's answer:

import numpy as np
from scipy.stats import ttest_ind
from scipy.stats import t
import pandas as pd

def welch_ttest(x1, x2,alternative):
    
    n1 = x1.size
    n2 = x2.size
    m1 = np.mean(x1)
    m2 = np.mean(x2)
    
    v1 = np.var(x1, ddof=1)
    v2 = np.var(x2, ddof=1)
    
    pooled_se = np.sqrt(v1 / n1 + v2 / n2)
    delta = m1-m2
    
    tstat = delta /  pooled_se
    df = (v1 / n1 + v2 / n2)**2 / (v1**2 / (n1**2 * (n1 - 1)) + v2**2 / (n2**2 * (n2 - 1)))
    
    # two side t-test
    p = 2 * t.cdf(-abs(tstat), df)
    
    # upper and lower bounds
    lb = delta - t.ppf(0.975,df)*pooled_se 
    ub = delta + t.ppf(0.975,df)*pooled_se
  
    return pd.DataFrame(np.array([tstat,df,p,delta,lb,ub]).reshape(1,-1),
                         columns=['T statistic','df','pvalue 2 sided','Difference in mean','lb','ub'])

We run this function, i named the lower and upper bounds of the 95% CI as lb and ub.. You can simply modify them in the function:

welch_ttest(ts1,ts2,"equal")


    T statistic     df  pvalue 2 sided  Difference in mean  lb  ub
0   -1.832542   17.90031    0.08356     -1.0    -2.146912   0.146912
2 of 4
5

Not sure about Scripy. Maybe there's a Scripy help site that will show the code. [Perhaps this.]

In R, a 95% CI is part of t.test output, where the Welch version of the 2-sample t test is the default (and argument var.eq=T gets you the pooled test).

ts1 = c(11,9,10,11,10,12,9,11,12,9)
ts2 = c(11,13,10,13,12,9,11,12,12,11)
t.test(ts1, ts2)

        Welch Two Sample t-test

data:  ts1 and ts2
t = -1.8325, df = 17.9, p-value = 0.08356
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -2.1469104  0.1469104
sample estimates:
mean of x mean of y 
     10.4      11.4 

Because the 95% CI includes the 2-sided test does not reject at the 5% level.

The 95% margin of error is where cuts probability from the upper tail of Student's t distribution with degrees of freedom as found from the Welch formula involving sample variances and sample sizes. [Here, in some software rounded down to an integer. One always has

me = qt(.975, 17.9)*sqrt(var(ts1)/10+var(ts2)/10); me
[1] 1.146912
pm=c(-1,1)
-1 + pm*me
[1] -2.1469118  0.1469118

It's always a good idea to keep the actual formulas in mind, even if one hopes to use them only rarely.

🌐
Penn State Statistics
online.stat.psu.edu › stat415 › lesson › 3 › 3.2
3.2 - Welch's t-Interval | STAT 415
Do the same thing for the Second variable (menneus data, for us), that is, type the Sample size, Mean, and Standard deviation in the appropriate boxes. Select Ok: The confidence interval output will appear in the session window. Here's what the output looks like for the spider and prey example with the confidence interval circled in red: ... Difference = mu (1) - mu (2) Estimate for difference: 1.240 95% CI for difference: (-0.870, 3.350) T-Test of difference = 0 (vs not =): T-Value = 1.25 P-Value = 0.231 DF = 16
🌐
Calculator.net
calculator.net › home › math › confidence interval calculator
Confidence Interval Calculator
Calculator to compute the confidence interval or margin of error of a sample based on the desired confidence level. It also provides an error bar diagram.
Find elsewhere
🌐
ThoughtCo
thoughtco.com › sample-t-test-confidence-interval-example-4022456
Two Sample T-Tests and Confidence Intervals
May 1, 2025 - We could use the T.INV function in Excel to calculate this value. We now put everything together and see that our margin of error is 2.09 x 1.2583, which is approximately 2.63. The confidence interval is 9 ± 2.63.
🌐
Select-statistics
select-statistics.co.uk › home › two-sample t-test
Two-Sample t-test - Select Statistical Consultants
February 24, 2016 - Use this calculator to test whether samples from two independent populations provide evidence that the populations have different means.
🌐
The BMJ
bmj.com › about-bmj › resources-readers › publications › statistics-square-one › 7-t-tests
7. The t tests
February 9, 2021 - In this case t 11 at P = 0.05 is 2.201 (table B) and so the 95% confidence interval is: -6.5 – 2.201 x 4.37 to -6.5 + 2.201 x 4.37 h. or -16.1 to 3.1h. This is quite wide, so we cannot really conclude that the two preparations are equivalent, and should look to a larger study.
🌐
Statistics LibreTexts
stats.libretexts.org › learning objects › interactive statistics
32: Two Independent Samples With Statistics Calculator - Statistics LibreTexts
December 28, 2023 - Enter in the statistics, the tail type and the confidence level and hit Calculate and the test statistic, t, the p-value, p, the confidence interval's lower bound, LB, and the upper bound, UB will be shown.
🌐
Penn State Statistics
online.stat.psu.edu › stat415 › book › export › html › 806
Lesson 3: Confidence Intervals for Two Means
Asking Minitab to calculate Welch's \(t\)-interval for \(\mu_X-\mu_Y\) require just a minor modification to the commands used in asking Minitab to calculate a two-sample pooled \(t\)-interval. We simply skip the step in which we click on the box Assume equal variances. Again, the commands required depend on whether the data are entered in two columns, or the data are entered in one column with a grouping variable in a second column. Since we've already learned how to ask Minitab to calculate a confidence interval for \(\mu_X-\mu_Y\) for both of those data arrangements, we'll take a look instead at the case in which the data are already summarized for us, as they are in the spider and prey example above.
🌐
Statisticslectures
statisticslectures.com › topics › ciindependentsamplest
Confidence Intervals for Independent Samples t-Test
Go to the t-table and look up the critical value for a two-tailed test, alpha = 0.05, and 29 degrees of freedom. You should find a value of 2.0452. Now, we can finish calculating the lower and upper bounds: We are 95% confident that the mean difference between sample 1 and sample 2 is between ...
🌐
Socscistatistics
socscistatistics.com › confidenceinterval › default2.aspx
Confidence Interval Calculator: Single-Sample T Statistic
This simple confidence interval calculator uses a t statistic and sample mean (M) to generate an interval estimate of a population mean (μ).
🌐
StatsKingdom
statskingdom.com › difference-confidence-interval-calculator.html
Calculates the confidence interval for the difference between two means - with calculation steps
CL -confidence level α = 1 - CL. T1 - α/2 - the t-score based on the t distribution, p(t < T1 - α/2) = 1 - α/2. df - degrees of freedom. The following R code should produce the same results. Find the means' difference confidence interval in the t.test results.
🌐
GIGACalculator.com
gigacalculator.com › calculators › statistics › confidence interval calculator
Confidence Interval Calculator
Use this confidence interval calculator to easily calculate the confidence bounds for a one-sample statistic or for differences between two proportions or means (two independent samples). One-sided and two-sided intervals are supported, as well as confidence intervals for relative difference (percent difference).
🌐
Statistics LibreTexts
stats.libretexts.org › learning objects › interactive statistics
34: Hypothesis Test and Confidence Interval Calculator for Two Dependent Samples - Statistics LibreTexts
December 28, 2023 - Two dependent Samples with data Calculator · Type in the values from the two data sets separated by commas, for example, 2,4,5,8,11,2. Then enter the tail type and the confidence level and hit Calculate and the test statistic, t, the p-value, ...
🌐
Penn State Statistics
online.stat.psu.edu › stat415 › lesson › 3 › 3.1
3.1 - Two-Sample Pooled t-Interval | STAT 415
Here's what the output looks like for the example above with the confidence interval circled in red: Difference = mu (1) - mu (2) Estimate for difference: 1.240 95% CI for difference: (-0.852, 3.332) T-Test of difference = 0 (vs not =): T-Value = 1.25 P-Value = 0.229 DF = 18 Both use Pooled ...
🌐
Stata
stata.com › links › stata-basics › two-sample-t-tests-calculator
Two-sample t-tests calculator | Stata
. ttesti 20 20 5 32 15 4, welch ... Pr(|T| > |t|) = 0.0006 Pr(T > t) = 0.0003 · You can use the level() option to change the level of the confidence interval....