You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

表格无法按理想格式调整,运行Kruskal检验代码报错求助

Fixing Kruskal-Wallis Test Error in Your Python Code

Let's break down why you're hitting that error and how to fix it quickly.

The Root Cause

Your line data = df.loc[:, :].tolist() converts your entire DataFrame into a row-wise nested list (each sublist represents one row of your data), but scipy.stats.kruskal() expects each independent sample as a separate input argument. In your experiment, each time column (10min, 20min, etc.) is a distinct sample—passing a single row-wise list doesn’t match the function’s requirements, which triggers the pandas-related error you’re seeing.

Corrected Code

First, let’s confirm your DataFrame is structured correctly (based on the data you provided):

#coding:utf-8
import scipy as sp
import scipy.stats
import pandas as pd
import numpy as np

# Reconstruct your DataFrame (adjust if your actual dataset has small differences)
df = pd.DataFrame({
    '10min': [6, 2, 3, 7],
    '20min': [7, 4, 9, 13],
    '30min': [9, 7, 18, 17],
    '40min': [15, 9, 25, 21],
    '50min': [21, 13, 34, 25],
    '60min': [30, 24, 49, 28]
})

# Extract each time column as an individual sample
samples = [df[col].values for col in df.columns]

# Pass all samples to kruskal using unpacking (*) to match the function's input format
ans = sp.stats.kruskal(*samples)

# Print the test result
print(ans)

Alternative Simplified Version

If you prefer a more direct approach, you can pass each column directly to the function without creating a separate list:

ans = sp.stats.kruskal(
    df['10min'], df['20min'], df['30min'],
    df['40min'], df['50min'], df['60min']
)

What This Does

  • We’re treating each time-based column as its own independent sample, which aligns with the Kruskal-Wallis test’s purpose: comparing distributions across multiple groups.
  • Using *samples unpacks the list of columns into individual arguments for kruskal(), which exactly matches the function’s expected input structure.

内容的提问来源于stack exchange,提问作者user9619086

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:15:50