首页 > 解决方案 > 使用两个不同大小的 pandas 数据框进行 Wilcoxon 测试

问题描述

我有两个不同长度的数据帧(一个是 16,另一个是 28)。我想使用scipy.stats.wilcoxon在这两者之间进行 Wilcoxon 测试。为此,我创建了一个函数:

def wilcoxon_test(df1, df2):

  list_col_1 = df1.columns
  list_col_2 = df2.columns

  for i in range(0, len(list_col_1)):
    name = list_col_1[i]
    for j in range(0, len(list_col_2)):
      name_check = list_col_2[j]
      if name_check == name:
        stat, pvalue = stats.wilcoxon(df1[name], df2[name_check])
        print("Wilcoxon test of {} and {}: stat = {}, pvalue = {}".format(name,name_check,stat,pvalue))
        if pvalue < 0.01:
          print("Pvalue between {} and {} < 0.01".format(name,name_check))

  return None

当数据具有相同大小时它工作得很好,但我正在使用不同大小的 DataFrame,它给了我这个错误:ValueError: The samples x and y must have the same length.

我在这篇文章中看到在R 上讨论这个问题,你可以通过传递 pair: FALSE 来做到这一点。通过这样做,它相当于进行 Mann-Whitney 检验。

有没有办法在 Python 上使用 scipy.stats.wilocoxon 做同样的事情,或者我应该直接使用scipy.stats.mannwhitneyu吗?

谢谢

标签: pythonpandasscipy

解决方案


如果您想要非配对 wilcoxon 测试,mannwhitneyu 似乎是正确的选择。在 mannwhitneyu 的 scipy 文档中,您可以找到以下描述:mannwhitneyu is for independent samples. For related / paired samples, consider scipy.stats.wilcoxon.


推荐阅读