pandas 中的双边 t 检验

two sided t-test in pandas

如果我有这样的 df:

        normalized_0  normalized_1  normalized_0   mean      std
Site                           
0           NaN      0.798262      1.456576       0.888687  0.118194
1      0.705540      0.885226           NaN       0.761488  0.047023
2      0.669539      1.002526      1.212976       0.826657  0.077940
3      0.829826      0.968180      0.988679       0.871290  0.032367

如何计算 0、1、2 与 3 的双侧 t 检验?

我试过:

from scipy.stats import ttest_ind

df['ttest'] = ttest_ind(df, d.loc[3])

但这不起作用...我得到的错误是:

TypeError: unsupported operand type(s) for /: 'str' and 'int'

你会如何解决这个问题?

我的回答可能完全不对,因为我只读过 t 检验:)

我从你的问题中了解到,你有一个 table,其中包含标准化值及其描述性统计数据(均值、标准差)。

此 table 中的每个索引值都是您分析的 category,您想要比较类别 [0, 1, 2][3]

我还假设您只需要标准化值作为输入数组,不需要均值或标准差。


selected_data = df.copy()
selected_data = selected_data[['normalized_0', 'normalized_1', 'normalized_0.1']]
selected_data['ttest'] = [ttest_ind(a=selected_data.iloc[3, :].values, \
                                    b=selected_data.iloc[x, :].values, \
                                    nan_policy='omit') for x in np.arange(len(selected_data))]

df.join(selected_data['ttest'])
        normalized_0  normalized_1  normalized_0.1 mean      std       ttest 
Site                           
0           NaN      0.798262      1.456576       0.888687  0.118194  (-0.7826642930343911, 0.4909212050511221)
1      0.705540      0.885226           NaN       0.761488  0.047023  (1.4370158341444121, 0.24625840339538163)  
2      0.669539      1.002526      1.212976       0.826657  0.077940  (-0.19764518466194855, 0.8529602343240825)
3      0.829826      0.968180      0.988679       0.871290  0.032367  (0.0, 1.0)

ab 参数是所选列的行值

# values of third category for example
selected_data.iloc[3, :].values 
# array([0.829826, 0.96818 , 0.988679])

omit是在计算测试时忽略nan值(默认情况下nan_policy的参数设置为propagate,如果存在任何缺失值,则returns nan ).