从列表中指定函数中的参数
Specifying arguments in a function from a list
我已经阅读了很多话题 (here and here) and the docs (here and here)。但是,我无法让它工作。我收到
错误
AxisError: axis 0 is out of bounds for array of dimension 0
谢谢。
import pandas as pd
from scipy.stats import levene
data = {'A': [1,2,3,4,5,6,7,8],
'B': [9,10,11,12,13,14,15,16],
'C': [1,2,3,4,5,6,7,8]}
df3 = pd.DataFrame(data, columns=['A', 'B','C'])
print(levene(df3['A'], df3['C'])) # this works as intended
cols_of_interest = ['A','C'] # my requirement could make this any combination
# function to pass through arguments into Levene test
def func(df,cols_of_interest):
cols = [col for col in 'df.'+df[cols_of_interest].columns] # my strategy to mimic the arguments
lev = levene(*cols)
print(lev)
func(df3,cols_of_interest)
将 def
中的列表理解替换为:
cols = [df[x] for x in cols_of_interest]
我已经阅读了很多话题 (here and here) and the docs (here and here)。但是,我无法让它工作。我收到
错误AxisError: axis 0 is out of bounds for array of dimension 0
谢谢。
import pandas as pd
from scipy.stats import levene
data = {'A': [1,2,3,4,5,6,7,8],
'B': [9,10,11,12,13,14,15,16],
'C': [1,2,3,4,5,6,7,8]}
df3 = pd.DataFrame(data, columns=['A', 'B','C'])
print(levene(df3['A'], df3['C'])) # this works as intended
cols_of_interest = ['A','C'] # my requirement could make this any combination
# function to pass through arguments into Levene test
def func(df,cols_of_interest):
cols = [col for col in 'df.'+df[cols_of_interest].columns] # my strategy to mimic the arguments
lev = levene(*cols)
print(lev)
func(df3,cols_of_interest)
将 def
中的列表理解替换为:
cols = [df[x] for x in cols_of_interest]