表格无法按理想格式调整,运行Kruskal检验代码报错求助
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
*samplesunpacks the list of columns into individual arguments forkruskal(), which exactly matches the function’s expected input structure.
内容的提问来源于stack exchange,提问作者user9619086

