简体   繁体   English

熊猫数据框上的成对检验统计显着性

[英]Pair-wise testing statistical significance on pandas data frame

I have a pandas dataframe (100x10), where each column represents some quantity and I would like to pair-wise test all columns using t-test. 我有一个pandas数据框(100x10),其中每列代表一定数量,我想使用t检验成对测试所有列。 Instead of looping over the columns: 而不是遍历列:

stats.ttest_rel(df.iloc[:,i], df.iloc[:,j])

where i!=j , is there a cleaner way to do it? i!=j ,有没有更清洁的方法? Something similar to correlations: 与关联类似:

df.corr()

where it computes all pair-wise correlations. 计算所有成对相关。

I think there is no direct method to create pair-wise t-test, you can try this one 我认为没有直接的方法可以创建成对t检验,您可以尝试一下

from scipy.stats import ttest_ind
import pandas as pd
import csv
df=pd.read_csv('input.csv')


fo = open('result.csv','wb+')
outfile = csv.writer(fo, delimiter=',')
outfile.writerow((df.columns).insert(0,''))

for i in df.columns:
    t=[]
    for j in df.columns:

        t.append(ttest_ind(df[i], df[j]))
    (t).insert(0,i)
    outfile.writerow(t)

this script will give you output file as result.csv that tells your pair-wise t-test calculation 该脚本将为您提供输出文件result.csv,告诉您成对的t检验计算

No need to do a double for-loop yourself. 无需自己做双重循环。 You can use itertools.combinations 您可以使用itertools.combinations

results = pd.DataFrame(columns=df.columns, index=df.columns)
for (label1, column1), (label2, column2) in itertools.combinations(df.items(), 2):
    results.loc[label1, label2] = results.loc[label2, label1] = stats.ttest_rel(column1, column2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM