【问题标题】:Transforming a Dataframe for statsmodels t-test为 statsmodels t 检验转换数据框
【发布时间】:2015-06-13 23:37:24
【问题描述】:

我正在尝试在 pandas/statsmodels 中运行 t 检验以比较两组之间的性能差异,但我很难以 statsmodels 可以使用的方式(以合理的方式)格式化数据。

我的 pandas 数据框目前如下所示:

Treatment      Performance
a              2
b              3
a              2
a              1
b              0

据我了解,要执行 t 检验,我需要按治疗组织的数据,如下所示:

TreatmentA    TreatmentB
2             3
2             0
1

这段代码几乎可以解决问题:

cat1 = df.groupby('Treatment', as_index=False).groups['a']
cat2 = df.groupby('Treatment', as_index=False).groups['b']
print(ttest_ind(cat1, cat2))

但是当我打印时,它看起来像是在拉动发生该处理的索引而不是性能值:

print(cat1)
[0, 2, 4, 5, 9, 10, 11, 16, 18,...131, 133, 142, 147, 152, 153, 156, 157, 158]

它[也许?]需要更像这样:

print(cat1)
[2, 2, 1, ...0, 3, 1, 1, 0, 2, 0, 0, 0]

将此数据帧转换为我可以执行 t 检验的格式的最佳方法是什么?

【问题讨论】:

    标签: python pandas statistics statsmodels hypothesis-test


    【解决方案1】:

    我认为最简单的方法是这样做:

    ttest_ind(df[df['Treatment'] == 'a']['Performance'], df[df['Treatment'] == 'b']['Performance'])
    

    希望对你有帮助。

    【讨论】:

    • 是的,看来可以!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 2018-07-28
    相关资源
    最近更新 更多